iOS使用NSMutableAttributedString實現富文本小結

来源:https://www.cnblogs.com/jiuyi/archive/2019/03/07/10492009.html
-Advertisement-
Play Games

NSAttributedString NSAttributedString對象管理適用於字元串中單個字元或字元範圍的字元串和關聯的屬性集(例如字體和字距)。NSAttributedString對象的預設字體是Helvetica 12點,可能與平臺的預設系統字體不同。因此,您可能希望創建適用於您的應用 ...


NSAttributedString

NSAttributedString對象管理適用於字元串中單個字元或字元範圍的字元串和關聯的屬性集(例如字體和字距)。NSAttributedString對象的預設字體是Helvetica 12點,可能與平臺的預設系統字體不同。因此,您可能希望創建適用於您的應用程式的非預設屬性的新字元串。您還可以使用NSParagraphStyle類及其子類NSMutableParagraphStyle來封裝NSAttributedString類使用的段落或標尺屬性。

實例化方法和使用方法

實例化方法

使用字元串初始化

- (instancetype)initWithString:(NSString *)str;

代碼示例

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"測試數據"];

字典中存放一些屬性名和屬性值

- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs;

代碼示例

NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0],
                                     NSForegroundColorAttributeName:[UIColor redColor],
                                     NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
                                     };
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"測試數據" attributes:attributedDict];

使用NSAttributedString初始化,與NSMutableString,NSString類似

- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

使用方法

為某一範圍內的文字設置多個屬性的方法

- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;

//代碼示例

NSString *string = @"測試數據";
NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0],
                                     NSForegroundColorAttributeName:[UIColor redColor],
                                     NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
                                     };
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
[attributedString setAttributes:attributedDict range:NSMakeRange(0, string.length)];

為某一範圍內的文字添加某個屬性的方法

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

//代碼示例

NSString *string = @"測試數據";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)];

為某一範圍內的文字添加多個屬性的方法

- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;

//代碼示例

NSString *string = @"測試數據";
NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0],
                                     NSForegroundColorAttributeName:[UIColor redColor],
                                     NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
                                     };
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];

移除某個範圍內的某個屬性的方法

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

//代碼示例

 NSString *string = @"測試數據";
 NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0],
                                     NSForegroundColorAttributeName:[UIColor redColor],
                                     NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
                                     };
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];
  
    
[attributedString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, string.length)];

屬性及說明

key說明
NSFontAttributeName 字體,value是UIFont對象
NSParagraphStyleAttributeName 繪圖的風格(居中,換行模式,間距等諸多風格),value是NSParagraphStyle對象
NSForegroundColorAttributeName 文字顏色,value是UIColor對象
NSLigatureAttributeName 字元連體,value是NSNumber
NSKernAttributeName 字元間隔
NSStrikethroughStyleAttributeName 刪除線,value是NSNumber
NSUnderlineStyleAttributeName 下劃線,value是NSNumber
NSStrokeColorAttributeName 描繪邊顏色,value是UIColor
NSStrokeWidthAttributeName 描邊寬度,value是NSNumber
NSShadowAttributeName 陰影,value是NSShadow對象
NSTextEffectAttributeName 文字效果,value是NSString
NSAttachmentAttributeName 附屬,value是NSTextAttachment 對象
NSLinkAttributeName 鏈接,value是NSURL or NSString
NSBaselineOffsetAttributeName 基礎偏移量,value是NSNumber對象
NSStrikethroughColorAttributeName 刪除線顏色,value是UIColor
NSObliquenessAttributeName 字體傾斜
NSExpansionAttributeName 字體扁平化
NSVerticalGlyphFormAttributeName 垂直或者水平,value是 NSNumber,0表示水平,1垂直

富文本段落排版格式屬性說明

屬性說明
lineSpacing 字體的行間距
firstLineHeadIndent 首行縮進
alignment (兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然)
lineBreakMode 結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
headIndent 整體縮進(首行除外)
minimumLineHeight 最低行高
maximumLineHeight 最大行高
paragraphSpacing 段與段之間的間距
paragraphSpacingBefore 段首行空白空間
baseWritingDirection 書寫方向(一共三種)
hyphenationFactor 連字屬性 在iOS,唯一支持的值分別為0和1



作者:coder小鵬


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 今天給既有Windows Cluster 添加節點時,驗證總是不通過。報錯信息為 防火牆未正確配置為故障轉移群集。現將處理步驟彙總如下。 1.錯誤具體信息 報錯的位置 --【驗證警告】 的步驟中發現錯誤 驗證發現故障,主要是說網路驗證時發現問題。此時,不能再進行下一步了。 打開生成的故障轉移集群驗證 ...
  • 安裝mysql的時候一直出現這個問題,在網上找了很多種方法,終於解決了這個問題。 我在官網下載的安裝包解壓後沒有my.ini文件,需要自己添加(紅字不要複製) [mysql]# 設置mysql客戶端預設字元集default-character-set=utf8[mysqld]# 設置3306埠po ...
  • 這是我們開啟了bin-log, 我們就必須指定我們的函數是否是1 DETERMINISTIC 不確定的2 NO SQL 沒有SQl語句,當然也不會修改數據3 READS SQL DATA 只是讀取數據,當然也不會修改數據4 MODIFIES SQL DATA 要修改數據5 CONTAINS SQL ...
  • [20190306]共用服務模式與SDU.txt--//一些文檔提到共用服務模式,服務端SDU=65535,測試驗證看看.--//鏈接:https://blogs.sap.com/2013/02/07/oracle-sqlnet-researching-setting-session-data-un ...
  • 查看更多寶典,請點擊《金三銀四,你的專屬面試寶典》 第一章:mysql資料庫 1)mysql與mariaDb MariaDB資料庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL授權許可 MariaDB的目的是完全相容MySQL,包括API和命令行,使之能輕鬆成為MySQL的代替品。 ...
  • 查看已存在的資料庫 建庫 刪庫 建表 修改表名 修改欄位名 修改欄位屬性 刪除欄位 刪表 ps:刪除有關聯的表時,先刪除子表再刪父表 建視圖 刪視圖 ps:刪除視圖,必須有DROP許可權 整理以上參考的文章鏈接如下: https://www.cnblogs.com/sqbk/p/5806797.htm ...
  • 【錯誤】 【問題分析】 line 3: 定義 NUM_VAL varchar2(500); line 9: NUM_VAL := 'NUM'+1; NUM_VAL是一個varchar類型的數據,而在資料庫語法中,字元串拼接要使用 || ,如果使用 + 則預設為數值類型。在使用java、c#、js一般 ...
  • 前文 "通用型的中文編程語言探討之一: 高考" 是基於現有英文編程語言的一個技術思路. 在 "這個回答" 以及下麵的討論中, 又提到了編程其實是人機交互的一種形式. 不禁試著跳出程式員視角看這個問題. 幾年前才發現Mozilla的火狐瀏覽器早在2008年開始了一個叫Ubiquity的項目, 而且根據 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...