iOS中自動斷字(hyphenationFactor)的使用
專案中某個cell需要顯示一個長文字,要求是中英文都要顯示。英文如果在行尾需要用-拆分為兩個。我們知道如果要把英文拆分,應該用NSLineBreakByCharWrapping,
於是我們寫了如下程式碼:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:content];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//兩邊對齊
paragraphStyle.alignment = NSTextAlignmentJustified;
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
CGFloat offset = 2;
[paragraphStyle setLineSpacing:offset];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
self.carName.attributedText = attributedString;
但是如果文字過長,就會發現有的顯示不出來,如圖:
(照顧隱私,加了馬賽克)
於是我們想起來,應該用NSLineBreakByTruncatingTail在文字末尾顯示省略號,然而,然而,NSLineBreakByTruncatingTail 和NSLineBreakByCharWrapping,這兩個是同一個屬性的, 我們檢視下NSParagraphStyle的lineBreakMode這個屬性,發現並不是位操作符。這可咋整啊。。
經過反覆查詢,功夫不負有心人。找到了一篇文章。
UILabel中英文顯示折行
發現了paragraphStyle的hyphenationFactor這個屬性。
在 Apple 開發者參考文件中,我們能找到 TextKit 有關自動斷字
hyphenationFactor屬性的描述:
檢視下文件中
Hyphenation is attempted when the ratio of the text width (as broken without hyphenation) to the width of the line fragment is less than the hyphenation factor. When the paragraph’s hyphenation factor is 0.0, the layout manager’s hyphenation factor is used instead. When both are 0.0, hyphenation is disabled. This property detects the user-selected language by examining the first item in preferredLanguages.
Soga,這個是連字元屬性,取值 0 到 1 之間,可以斷詞。(具體設定為0.幾,文章中說了,我也沒太看明白。於是我就設定了0.7)
在上面的程式碼中加上這句話:
paragraphStyle.hyphenationFactor = 0.7;
重新跑一遍。。。。。。
為啥不好使!
繼續按關鍵詞hyphenationFactor找吧。找到了這篇文章
按照文章中,給NSLocale加了個Category,將preferredLanguages轉換為英文。
#import "NSLocale+ForceHyphenation.h"
@implementation NSLocale (ForceHyphenation)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector = @selector(preferredLanguages);
SEL swizzledSelector = @selector(sr_preferredLanguages);
Class class = object_getClass((id)self);
Method originalMethod = class_getClassMethod(class, originalSelector);
Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
+ (NSArray <NSString *> *)sr_preferredLanguages {
[self sr_preferredLanguages];
return @[ @"en" ];
}
@end
build..
run..
...
好激動,終於成功了。。
???????
寫這篇文章,希望有相同遭遇的同學能看到吧。
另外,利用YYText設定truncationToken可以完美的實現這項功能,而且效率比這個高,感興趣的同學可以看下下面的demo:
https://github.com/zgsddzwj/WJLabelDemo
本人QQ:297959735 郵箱:zgsddzwj@163.com,歡迎提意見。
相關文章
- iOS自動化打包(fastlane使用)iOSAST
- UI自動化使用斷言示例1UI
- 使用Automator自動化iOS打包流程iOS
- iOS自動化測試之KIF使用分享iOS
- python實現自動提取句子中的關鍵字Python
- iOS中WKWebView互動使用總結iOSWebView
- IOSHTML5頁面中數字自動變藍並識別為手機號iOSHTML
- [iOS 取出字串中的數字]iOS字串
- BIOS INT 10中斷功能iOS
- Python的iOS自動化打包PythoniOS
- IOS中的動畫使用iOS動畫
- iOS中RAC的使用iOS
- AppUI 自動化中的影像識別的使用APPUI
- iOS中AutoLayer自動佈局流程及相關方法iOS
- SecureCRT使用SSH連線linux超時後自動斷開SecurecrtLinux
- Oracle ADDM 自動診斷監視工具介紹及使用Oracle
- 如何用sql找出中間斷號的數字SQL
- iOS動態庫的使用iOS
- iOS 9 關鍵字的簡單使用iOS
- iOS自動打包指令碼+自動上傳到firiOS指令碼
- 來自銳動天地的直播ios SDKiOS
- iOS多target下的自動構建iOS
- SMP PPI中斷使用
- iOS中陀螺儀的使用iOS
- 電腦開機自動斷電怎麼辦 電腦自動斷電解決辦法
- [轉]路由器老是自動斷線的原因路由器
- 自管理的資料庫:自動效能診斷資料庫
- [分享]iOS開發 - iOS自動佈局的替換方案iOS
- Java中的自動裝箱與自動拆箱Java
- 使用 Fastlane 實現 iOS 跟 Android 自動打包指令碼ASTiOSAndroid指令碼
- Java 判斷字串中是否含有漢字.Java字串
- iOS中的static const extern關鍵字iOS
- 字尾自動機學習
- appium 自動化中關於斷言問題,有沒有更好的斷言方法,求大神賜教!APP
- Flutter iOS 混合工程自動化FlutteriOS
- iOS自動化編譯打包iOS編譯
- iOS開發-自動打包神器iOS
- iOS逆向-微信自動新增好友iOS