1. 程式人生 > >iOS 富文字label字型大小和顏色

iOS 富文字label字型大小和顏色

實現:最初實現的時候想到了用兩個Label,來實現,第一個顯示¥4000,設定一個字型,第二個顯示/月,設定另一個字型.這樣就能實現這個效果了,但是最後想一想還是用富文字比較好,順便可以學習一下.

//先建立一個label:

-(UILabel *)priceLabel{

 if (_priceLabel == nil)

{

_priceLabel = [[UILabel alloc]init];

_priceLabel.font = kFONT(13);

_priceLabel.textColor = kColorTheme;

_priceLabel.textAlignment = NSTextAlignmentRight;

}

return _priceLabel;

}

_priceLabel.attributedText = [self getPriceAttribute:@"¥4000/月"]; 

-(NSMutableAttributedString *)getPriceAttribute:(NSString *)string{

NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc]initWithString:string];

//目的是想改變 ‘/’前面的字型的屬性,所以找到目標的range

NSRange range = [string rangeOfString:@"/"];

NSRange pointRange = NSMakeRange(0, range.location);

NSMutableDictionary *dic = [NSMutableDictionary dictionary];

dic[NSFontAttributeName] = [UIFont systemFontOfSize:18];

//賦值 [attribut addAttributes:dic range:pointRange];

return attribut;

}