2017-07-27 95 views
-2

設計標籤如何在UILabel中顯示貨幣值?

designed label

我想設計標籤作爲單個標籤顯示的圖像上。

任何人都可以幫助我嗎?

+1

退房'NSAttributedString'。 – the4kman

+0

使用'NumberFormatter'或'NSAttributedString'。 –

+0

我試過NSAttributedString,但數字並排設置,但不是如圖所示 –

回答

0

在雨燕3.0

let fnt = UIFont(name: "Helvetica", size: 50) 
    let attributedString = NSMutableAttributedString(string: "$499", attributes: [NSFontAttributeName: fnt?.withSize(50) as Any]) 
    attributedString.setAttributes([NSFontAttributeName: fnt?.withSize(20) as Any, NSBaselineOffsetAttributeName: 20], range: NSRange(location: 2, length: 2)) 
    label.attributedText = attributedString 
0

使用屬性字符串並給「99」更小的字體大小和更高的基準。下面是斯威夫特4的例子(對不起,我沒有時間翻譯成向後斯威夫特3本或更早):

let s = "$499" 
let mas = NSMutableAttributedString(string: s, attributes: [ 
    .foregroundColor : UIColor.white, 
    .font : UIFont(name:"Helvetica", size:50)! 
]) 
let r2 = s.index(before: s.endIndex) 
let r1 = s.index(before: r2) 
let r = NSRange(r1...r2, in:s) 
mas.addAttributes([ 
    .font : UIFont(name:"Helvetica", size:20)!, 
    .baselineOffset : 20 
    ], range: r) 

現在賦值給標籤的attributedText。與其他細節玩弄是你。

enter image description here