我可以使用此方法获取字符串大小
[myString sizeWithAttributes:@{NSFontAttributeName :myFont}
但是,我的字符串有 HTML 内容,我尝试这样做
[myString sizeWithAttributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName :myFont}
但是,我得到了错误的值?
我也尝试像这样使用 NSAttributedString
NSAttributedString * tabText = [[NSAttributedString alloc] initWithData:[myString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,myFont: NSFontAttributeName} documentAttributes:nil error:nil];
CGSize mySize=[tabText size];
而且我得到了错误的值。
我的字符串是非常基本的 HTML 内容,例如它返回的“test & test
”
宽度为 15 个字符而不是 11
最佳答案
首先尝试删除 html 标签,以此作为引用 - Remove HTML Tags from an NSString on the iPhone
然后获取字符串大小,试试这个:
CGSize stringSize = [YOUR_STRING sizeWithAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
// ceilf - is used to get equivalent values
CGSize currentSize = CGSizeMake(ceilf(stringSize.width), ceilf(stringSize.height));
关于ios - 如何获取带有html内容的字符串的字符串大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33409717/