在實際的應用開發中經常需要使用到富文本,這里總結經常使用到的富文本屬性方便學習與記憶
NSFontAttributeName:指定字體大小,默認是12point;UIFont
代碼實例:
NSMutableAttributedString *originString = [[NSMutableAttributedString alloc] initWithString:@"默認字體樣式\n" attributes:nil]; //增加NSFontAttributeName屬性設置字體大小為18 NSAttributedString *fontAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSFontAttributeName樣式的字體\n" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}]; [originString appendAttributedString:fontAttributeStr];
NSForegroundColorAttributeName:指定字體顏色,默認是黑色;UIColor
代碼實例:
//增加NSForegroundColorAttributeName屬性設置字體顏色為紅色 NSAttributedString *textColorAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSForegroundColorAttributeName樣式的字體\n" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}]; [originString appendAttributedString:textColorAttributeStr];
NSBackgroundColorAttributeName:指定字體背景顏色;UIColor
代碼實例:
NSAttributedString *backColorAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSBackgroundColorAttributeName樣式的字體\n" attributes:@{NSBackgroundColorAttributeName:[UIColor redColor]}]; [originString appendAttributedString:backColorAttributeStr];
NSLigatureAttributeName:連體字符設置,沒用過暫時;NSNumber
NSKernAttributeName:設置字體間距;NSNumber
代碼實例:
NSAttributedString *textSpaceAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSKernAttributeName樣式的字體\n" attributes:@{NSKernAttributeName:@3}]; [originString appendAttributedString:textSpaceAttributeStr];
NSStrikethroughStyleAttributeName:設置刪除線樣式,其取值如下:NSNumber
取值 | 對應樣式 |
---|---|
NSUnderlineStyleNone | 不設置刪除線 |
NSUnderlineStyleSingle | 一條較細的刪除線 |
NSUnderlineStyleThick | 一條較粗的刪除線 |
NSUnderlineStyleDouble | 兩條較細的刪除線 |
NSUnderlineStyleDouble | 兩條較細的刪除線 |
代碼實例:
NSAttributedString *deleteAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSStrikethroughStyleAttributeName樣式的字體\n" attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),NSStrikethroughColorAttributeName:[UIColor redColor]}]; [originString appendAttributedString:deleteAttributeStr];
- NSStrikethroughColorAttributeName:設置刪除線的顏色;UIColor
- NSUnderlineStyleAttributeName ** 和 NSUnderlineColorAttributeName:設置下劃線和下劃線的顏色;NSNumber** && UIColor下劃線樣式取值與NSStrikethroughStyleAttributeName使用相同的枚舉;
-
NSStrokeWidthAttributeName 和 NSStrokeColorAttributeName :字體描邊以及描邊顏色的設置;NSNumber && UIColor
代碼實例:
//設置字體描邊以及描邊顏色 NSAttributedString *strokeAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSStrokeWidthAttributeName樣式的字體\n" attributes:@{NSStrokeWidthAttributeName:@1,NSStrokeColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:18 weight:UIFontWeightBold]}]; [originString appendAttributedString:strokeAttributeStr];
-
NSShadowAttributeName:設置文字陰影; NSShadow
代碼實例:
//設置文字陰影,使用NSShadow NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowBlurRadius = 3.0f; //模糊 shadow.shadowColor = [UIColor redColor];//顏色 shadow.shadowOffset = CGSizeMake(1, 5); NSAttributedString *shadowAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSShadowAttributeName樣式的字體\n" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSShadowAttributeName:shadow}]; [originString appendAttributedString:shadowAttributeStr];
-
NSBaselineOffsetAttributeName:設置文字基于準線上下偏移,其值為正數時上移,反之下移 NSNumber
代碼實例:
//設置文字偏移 NSAttributedString *baselineOffsetAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSBaselineOffsetAttributeName樣式的字體\n" attributes:@{NSBaselineOffsetAttributeName:@5}]; //方便對比加入正常的字符串 [originString appendAttributedString:[[NSAttributedString alloc] initWithString:@"沒有設置偏移的" attributes:nil]]; [originString appendAttributedString:baselineOffsetAttributeStr];
-
NSWritingDirectionAttributeName:設置文字書寫方向 NSArray of NSNumber
代碼實例:
//設置文字書寫方向:NSWritingDirectionAttributeName NSAttributedString *directionAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSWritingDirectionAttributeName樣式的字體\n" attributes:@{NSWritingDirectionAttributeName:@[@3]}]; [originString appendAttributedString:directionAttributeStr];
-
NSLinkAttributeName:設置點擊文字的跳轉鏈接,需要使用UITextView并實現其代理方法shouldInteractWithURL NSURL or NSString
代碼實例:
//設置點擊跳轉鏈接:NSLinkAttributeName NSAttributedString *linkAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSLinkAttributeName樣式的字體\n" attributes:@{NSLinkAttributeName:@"https://www.baidu.com"}]; [originString appendAttributedString:linkAttributeStr];
-
NSTextAttachment:文本附件,常被用來實現圖文混排 NSTextAttachment
代碼實例:
//圖文混排 NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://upload.jianshu.io/users/upload_avatars/2115199/148387f72be4.jpg?imageMogr/thumbnail/240x240/quality/100"]]]; attachment.bounds = CGRectMake(0, 0, 30, 30); NSAttributedString *attachmentImg = [NSAttributedString attributedStringWithAttachment:attachment]; NSAttributedString *attachmentAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSTextAttachment樣式的字體\n" attributes:nil]; [originString appendAttributedString:attachmentImg]; [originString appendAttributedString:attachmentAttributeStr];