Voglio che il mio UILabel visualizzi il testo nel modo seguente 6.022 * 10 . Quali fumi sono rapidi per pedici e apici?Come utilizzare pedice e apice in Swift
risposta
La maggior parte delle risposte + esempi sono in ObjC, ma questo è come farlo in Swift.
let font:UIFont? = UIFont(name: "Helvetica", size:20)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "6.022*1023", attributes: [.font:font!])
attString.setAttributes([.font:fontSuper!,.baselineOffset:10], range: NSRange(location:8,length:2))
labelVarName.attributedText = attString
Questo mi dà:
In una spiegazione più dettagliata:
- Diventa
UIFont
si vuole sia il default e lo stile apice, apice deve essere più piccolo. - Creare uno
NSMutableAttributedString
con la stringa completa e il carattere predefinito. - Aggiungere un attributo ai caratteri che si desidera modificare (
NSRange
), con il più piccolo/pediceUIFont
e il valoreNSBaselineOffsetAttributeName
è l'importo che si desidera compensare verticalmente. - assegnarlo al proprio
UILabel
Speriamo che questo aiuta altri sviluppatori Swift come ho bisogno di questo pure.
Risposta perfetta, grazie !! –
Arresta in modo anomalo se Helvetica non è disponibile nelle dimensioni in punti 10 o 20. Anche il codice di esempio deve essere scritto con un minimo di gestione degli errori. Anche i nomi delle variabili lo rendono confuso e non viene compilato. –
Se si può andare d'accordo con il testo che non sembra perfetto, e solo bisogno di un sottoinsieme di caratteri che si possono fare uso della apice unicode e numeri pedice: ⁰ ¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ ₀ ₁ 2 ₃ ₄ ₅ ₆ ₇ ₈ ₉ Questo ha il vantaggio di essere molto meno ingombrante.
Un altro vantaggio di questo è che quando si hanno più linee, questo non incasina con il linepacing come lo spostamento della linea di base nella stringa attribuita. –
Come un approccio diverso, ho scritto una funzione che prende in una stringa in cui gli esponenti sono prefissati con ^
quali 2^2•3•5^2
e restituisce 2²•3•5²
func exponentize(str: String) -> String {
let supers = [
"1": "\u{00B9}",
"2": "\u{00B2}",
"3": "\u{00B3}",
"4": "\u{2074}",
"5": "\u{2075}",
"6": "\u{2076}",
"7": "\u{2077}",
"8": "\u{2078}",
"9": "\u{2079}"]
var newStr = ""
var isExp = false
for (_, char) in str.characters.enumerate() {
if char == "^" {
isExp = true
} else {
if isExp {
let key = String(char)
if supers.keys.contains(key) {
newStr.append(Character(supers[key]!))
} else {
isExp = false
newStr.append(char)
}
} else {
newStr.append(char)
}
}
}
return newStr
}
E 'un po' di un metodo di forza bruta, ma funziona se non vuoi gestire le string attribuite o vuoi che la tua stringa sia indipendente da un font.
Ho scritto la seguente estensione o puoi usarla come una funzione, sta funzionando bene per me. è possibile modificarlo saltando le parti che non sono essenziali per voi
extension NSMutableAttributedString
{
enum scripting : Int
{
case aSub = -1
case aSuper = 1
}
func characterSubscriptAndSuperscript(string:String,
characters:[Character],
type:scripting,
fontSize:CGFloat,
scriptFontSize:CGFloat,
offSet:Int,
length:[Int],
alignment:NSTextAlignment)-> NSMutableAttributedString
{
let paraghraphStyle = NSMutableParagraphStyle()
// Set The Paragraph aligmnet , you can ignore this part and delet off the function
paraghraphStyle.alignment = alignment
var scriptedCharaterLocation = Int()
//Define the fonts you want to use and sizes
let stringFont = UIFont.boldSystemFont(ofSize: fontSize)
let scriptFont = UIFont.boldSystemFont(ofSize: scriptFontSize)
// Define Attributes of the text body , this part can be removed of the function
let attString = NSMutableAttributedString(string:string, attributes: [NSFontAttributeName:stringFont,NSForegroundColorAttributeName:UIColor.black,NSParagraphStyleAttributeName: paraghraphStyle])
// the enum is used here declaring the required offset
let baseLineOffset = offSet * type.rawValue
// enumerated the main text characters using a for loop
for (i,c) in string.characters.enumerated()
{
// enumerated the array of first characters to subscript
for (theLength,aCharacter) in characters.enumerated()
{
if c == aCharacter
{
// Get to location of the first character
scriptedCharaterLocation = i
//Now set attributes starting from the character above
attString.setAttributes([NSFontAttributeName:scriptFont,
// baseline off set from . the enum i.e. +/- 1
NSBaselineOffsetAttributeName:baseLineOffset,
NSForegroundColorAttributeName:UIColor.black],
// the range from above location
range:NSRange(location:scriptedCharaterLocation,
// you define the length in the length array
// if subscripting at different location
// you need to define the length for each one
length:length[theLength]))
}
}
}
return attString}
}
esempi:
let attStr1 = NSMutableAttributedString().characterSubscriptAndSuperscript(
string: "23 x 456",
characters:["3","5"],
type: .aSuper,
fontSize: 20,
scriptFontSize: 15,
offSet: 10,
length: [1,2],
alignment: .left)
let attStr2 = NSMutableAttributedString().characterSubscriptAndSuperscript(
string: "H2SO4",
characters: ["2","4"],
type: .aSub,
fontSize: 20,
scriptFontSize: 15,
offSet: 8,
length: [1,1],
alignment: .left)
Molto bello. L'ho esteso un po 'aggiungendo un parametro 'textColor'. E ha sostituito 'UIFont.boldSystemFont' con' UIFont.systemFont'. – Isuru
Posso vedere la versione estesa @Isuru? –
Per un semplice da usare Swift soluzione, tu m ight vuole pagare HandyUIKit. Dopo averlo importato nel tuo progetto (ad es.via Cartagine - vedere le istruzioni in README) si può fare qualcosa di simile:
import HandyUIKit
"6.022*10^{23}".superscripted(font: UIFont.systemFont(ofSize: 20, weight: .medium))
Questa linea restituirà un NSAttributedString
che apparire esattamente come quello che stai cercando per. Solo assegnalo a a UILabel
s attributedText
proprietà e questo è tutto!
Se siete alla ricerca di indicizzazione di un testo, è sufficiente utilizzare subscripted(font:)
invece. Riconoscerà strutture come CO_{2}
. C'è anche superAndSubscripted(font:)
se si desidera combinare entrambi.
Vedere docs per ulteriori informazioni e ulteriori esempi.
questo pod è molto utile. grazie per questo –
Ecco una versione semplice che ha la corretta gestione degli errori e verrà compilata nel parco giochi.
import UIKit
func setMyLabelText(myLabel: UILabel) {
if let largeFont = UIFont(name: "Helvetica", size: 20), let superScriptFont = UIFont(name: "Helvetica", size:10) {
let numberString = NSMutableAttributedString(string: "6.022*10", attributes: [.font: largeFont])
numberString.append(NSAttributedString(string: "23", attributes: [.font: superScriptFont, .baselineOffset: 10]))
myLabel.attributedText = numberString
}
}
let myLabel = UILabel()
setMyLabelText(myLabel: myLabel)
- 1. Apice/pedice in collegamento ipertestuale in WPF
- 2. pedice e apice per lo stesso elemento
- 3. Set apice e pedice in testo formattato in WPF
- 4. Etichette degli assi in apice e pedice in ggplot2
- 5. pedice e apice in ylab di qplot [R]
- 6. kCTSuperscriptAttributeName non funziona per l'utilizzo di apice e pedice
- 7. visualizzare il contenuto su highcharts titolo Xaxis e Yaxis in forma di pedice e apice
- 8. Posso avere pedice statico in Swift?
- 9. Generico in pedice per rapida
- 10. apice/pedice nelle etichette per gnuplot leggere dal file di dati
- 11. Come utilizzare dequeueReusableCellWithIdentifier in Swift?
- 12. Come utilizzare JSTileMap in swift
- 13. Come utilizzare addAttribute in Swift
- 14. Come scrivere un apice in una stringa e visualizzarlo usando MessageBox.Show()?
- 15. Come utilizzare UILocalNotification in Swift
- 16. apice in stringa Java
- 17. Come utilizzare contemporaneamente apice e variabile in un'etichetta dell'asse con ggplot2
- 18. Valori scrittura pedice In VBA
- 19. Quando e come utilizzare l'attributo @noreturn in Swift?
- 20. HTML: posso inserire il testo di pedice proprio sotto l'apice?
- 21. Come utilizzare l'associazione opzionale in Swift 2
- 22. come utilizzare blocco/chiusura in swift
- 23. Come utilizzare un UISplitViewController in Swift
- 24. Apice in ggplot2 utilizzando plotmath
- 25. Come utilizzare sub e apice nel testo Mathematica, ad esempio: formule chimiche?
- 26. Apice in uscita console C++
- 27. Come utilizzare UILongPressGestureRecognizer con UICollectionViewCell in Swift?
- 28. Come utilizzare dispatch_queue_create in Swift 2.0?
- 29. Come utilizzare "enumerateChildNodesWithName" con Swift in SpriteKit?
- 30. Come utilizzare struct Swift in Objective C
Vedi http://stackoverflow.com/questions/8555735/how-to-make-subscripts-and-superscripts-using-nsattributedstring. –