富文本

在實際的應(yīng)用開發(fā)中經(jīng)常需要使用到富文本,這里總結(jié)經(jīng)常使用到的富文本屬性方便學(xué)習(xí)與記憶

  • NSFontAttributeName:指定字體大小,默認是12point;UIFont
    代碼實例:
    NSMutableAttributedString *originString = [[NSMutableAttributedString alloc] initWithString:@"默認字體樣式\n" attributes:nil]; //增加NSFontAttributeName屬性設(shè)置字體大小為18 NSAttributedString *fontAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSFontAttributeName樣式的字體\n" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}]; [originString appendAttributedString:fontAttributeStr];

  • NSForegroundColorAttributeName:指定字體顏色,默認是黑色;UIColor
    代碼實例:
    //增加NSForegroundColorAttributeName屬性設(shè)置字體顏色為紅色 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:連體字符設(shè)置,沒用過暫時;NSNumber

  • NSKernAttributeName:設(shè)置字體間距;NSNumber
    代碼實例:
    NSAttributedString *textSpaceAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSKernAttributeName樣式的字體\n" attributes:@{NSKernAttributeName:@3}]; [originString appendAttributedString:textSpaceAttributeStr];

  • NSStrikethroughStyleAttributeName:設(shè)置刪除線樣式,其取值如下:NSNumber

取值 對應(yīng)樣式
NSUnderlineStyleNone 不設(shè)置刪除線
NSUnderlineStyleSingle 一條較細的刪除線
NSUnderlineStyleThick 一條較粗的刪除線
NSUnderlineStyleDouble 兩條較細的刪除線
NSUnderlineStyleDouble 兩條較細的刪除線

代碼實例:
NSAttributedString *deleteAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSStrikethroughStyleAttributeName樣式的字體\n" attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),NSStrikethroughColorAttributeName:[UIColor redColor]}]; [originString appendAttributedString:deleteAttributeStr];

  • NSStrikethroughColorAttributeName:設(shè)置刪除線的顏色;UIColor
  • NSUnderlineStyleAttributeName ** 和 NSUnderlineColorAttributeName:設(shè)置下劃線和下劃線的顏色;NSNumber** && UIColor下劃線樣式取值與NSStrikethroughStyleAttributeName使用相同的枚舉;
  • NSStrokeWidthAttributeNameNSStrokeColorAttributeName :字體描邊以及描邊顏色的設(shè)置;NSNumber && UIColor
    代碼實例:
    //設(shè)置字體描邊以及描邊顏色 NSAttributedString *strokeAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSStrokeWidthAttributeName樣式的字體\n" attributes:@{NSStrokeWidthAttributeName:@1,NSStrokeColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:18 weight:UIFontWeightBold]}]; [originString appendAttributedString:strokeAttributeStr];
  • NSShadowAttributeName:設(shè)置文字陰影; NSShadow
    代碼實例:
    //設(shè)置文字陰影,使用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:設(shè)置文字基于準(zhǔn)線上下偏移,其值為正數(shù)時上移,反之下移 NSNumber
    代碼實例:
    //設(shè)置文字偏移 NSAttributedString *baselineOffsetAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSBaselineOffsetAttributeName樣式的字體\n" attributes:@{NSBaselineOffsetAttributeName:@5}]; //方便對比加入正常的字符串 [originString appendAttributedString:[[NSAttributedString alloc] initWithString:@"沒有設(shè)置偏移的" attributes:nil]]; [originString appendAttributedString:baselineOffsetAttributeStr];
  • NSWritingDirectionAttributeName:設(shè)置文字書寫方向 NSArray of NSNumber
    代碼實例:
    //設(shè)置文字書寫方向:NSWritingDirectionAttributeName NSAttributedString *directionAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSWritingDirectionAttributeName樣式的字體\n" attributes:@{NSWritingDirectionAttributeName:@[@3]}]; [originString appendAttributedString:directionAttributeStr];
  • NSLinkAttributeName:設(shè)置點擊文字的跳轉(zhuǎn)鏈接,需要使用UITextView并實現(xiàn)其代理方法shouldInteractWithURL NSURL or NSString
    代碼實例:
    //設(shè)置點擊跳轉(zhuǎn)鏈接:NSLinkAttributeName NSAttributedString *linkAttributeStr = [[NSAttributedString alloc] initWithString:@"帶有NSLinkAttributeName樣式的字體\n" attributes:@{NSLinkAttributeName:@"https://www.baidu.com"}]; [originString appendAttributedString:linkAttributeStr];
  • NSTextAttachment:文本附件,常被用來實現(xiàn)圖文混排 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];

以上所有樣式的效果圖如下:

Normal.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容