我在UILabel工作过.但是不推荐使用setLineBreakMode.我一直在使用NSAttributedString.但UILabel setLineBreakMode是之后UILabel setNumberOfLines else不起作用.
之前:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.text = @"XXXXXX";
memoLabel.textAlignment = UITextAlignmentLeft;
memoLabel.numberOfLines = 2;
memoLabel.lineBreakMode = UILineBreakModeTailTruncation;
memoLabel.font = [UIFont systemFontOfSize:11];
memoLabel.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
IOS 6之后:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSAttributedString *string
= [[NSAttributedString alloc] initWithString:text
attributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],
NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName,nil]];
[paragraphStyle release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.attributedText = string;
[string relase];
Run Code Online (Sandbox Code Playgroud)
我希望在显示之前和之后都一样.如何显示多行?
使用UITextView时,我尝试将自定义属性添加到属性字符串.但是,在分配到UITextView的attributedText后,所有自定义键都将丢失.像这样:
NSMutableAttributedString *lString = [[ NSMutableAttributedString alloc ] initWithString: @"astring"
attributes: @{ @"customkey": @"customvalue" }];
NSLog(@"string: %@", lString); // shows customkey present
textView.attributedText = lString;
NSLog(@"result: %@", self.textView.attributedText); // shows customkey missing
Run Code Online (Sandbox Code Playgroud)
这应该有用吗?
我正在创建一个iOS应用程序,我想显示一个带有UITextView中指定的特定制表位的属性字符串.我还想在drawRect中使用Core Text将它们直接绘制到UIViews中.我希望(如果可能的话)在两种情况下使用相同的属性字符串.
因此,在我的应用程序中,我创建了一个CFAttributedStringRef或NSAttributedString,并为其应用了一个CTParagraphStyle属性.然后,我尝试在UITextView中显示属性字符串,我得到如下所示的崩溃:
-[__NSCFType headIndent]: unrecognized selector sent to instance 0x7545080
po 0x7545080
$0 = 122966144 CTParagraphStyle:
base writing direction = -1, alignment = 4, line break mode = 0,
default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 0,
paragraph spacing before = 0
tabs:
( …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个UICollectionView的本土版本(我在UICollectionView存在之前编写它,并且它似乎比我的用例的UICollectionView更高效).它由滚动视图中的几个图块组成.
为了帮助滚动性能,并且因为每个图块由大约十二个PNG组成,每个图块使用NSOperations和NSOperationQueue预渲染到NSCache.没有这种优化,滚动性能很糟糕.
除了几个PNG之外,每个平铺都有(或可能有)几个文本.
每个NSOperation,以避免导致在后台线程上进行任何类型的渲染(当然,这是一个UIKit禁忌)的问题,使用CoreGraphics和NSAttributedStrings来渲染图像.
这让我想起了崩溃.崩溃(下面)似乎与NSAttributedString呈现有关.由于这个充满了平铺渲染操作的NSOperationQueue是我的应用程序中唯一使用NSAttributedStrings并被大量调用的地方,我不得不认为它是罪魁祸首.我们在崩溃报告中看到崩溃合理地发生,但通常不足以让我们在内部可靠地再现它(我们的应用程序拥有相当庞大的用户群).
令人着迷的是,崩溃只发生在iOS 7上.任何人都有任何想法吗?这是一个崩溃的例子:
Incident Identifier: 3BE1FF37-4288-40A9-954D-1AD909987D05
CrashReporter Key: 000339CB-9B5B-46EE-B256-4D911B190E7D
Hardware Model: iPhone5,2
Process: AppName [733]
Path: /Users/USER/AppName.app/AppName
Identifier: com.companyname.appname
Version: 2.0.1
Code Type: ARM
Parent Process: launchd [1]
Date/Time: 2013-10-03 23:32:20 +0000
OS Version: iPhone OS 7.0.2 (11A501)
Report Version: 104
Exception Type: SIGBUS
Exception Codes: BUS_ADRALN at 0x100078c
Crashed Thread: 0
Thread 0 Crashed:
0 ??? 0x0100078c 0x0 + 0
1 UIFoundation 0x36fbf027 -[NSLayoutManager _drawLineForGlyphRange:type:baselineOffset:lineFragmentRect:lineFragmentGlyphRange:containerOrigin:isStrikethrough:] + 3358
2 UIFoundation 0x36ffdd1f -[NSLayoutManager drawUnderlineForGlyphRange:underlineType:baselineOffset:lineFragmentRect:lineFragmentGlyphRange:containerOrigin:] + 86
3 …Run Code Online (Sandbox Code Playgroud) 我正在使用HTML内容呈现textview.我已经采用了一些"div"标签并在其上应用了一些CSS.我在使用CSS类在"div"上应用填充时遇到了一个问题,该类在textview上渲染后没有反映div.我使用以下方法将HTML内容字符串转换为属性字符串.
if let htmlData = htmlContent.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true) {
do {
return try NSMutableAttributedString(
data: htmlData,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
}
catch let error as NSError {
printDebuggingMode("Couldn't translate \(htmlContent): \(error.localizedDescription) ")
}
}
Run Code Online (Sandbox Code Playgroud)
我希望在textView的图像中实现下面附带的类似内容.
.padding {
padding-left: 20px;
}
Run Code Online (Sandbox Code Playgroud) 我一直在尝试解决毛刺表视图滚动性能问题.通过Instrument的Time Profiler运行它表明花费了相当多的时间将NSAttributedString设置为UILabel.这是一个已知的问题吗?如果是这样,有解决方法吗?
我看到一些报道称在iOS8上创建NSAttributedString的速度非常慢,并且在iOS9上有所改进,但它是否也将它设置为包含UI元素的速度慢?在iOS 8下,NSAttributedString性能更差
下面是我的分析屏幕截图.请注意,视图模型不会动态创建属性字符串,因此您看到的成本完全来自赋值.当然,UILabel可以执行昂贵的操作,我不知道如何绕行.
我感谢任何帮助.
我正在进行扩展,将html转换为属性字符串,代码为
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return htmlToAttributedString?.string ?? ""
}
Run Code Online (Sandbox Code Playgroud)
我收到以下3个相同的错误
使用未解析的标识符'NSDocumentTypeDocumentAttribute'
使用未解析的标识符'NSHTMLTextDocumentType'
使用未解析的标识符'NSCharacterEncodingDocumentAttribute'
我假设我犯了一个错误的语法导致3相同的错误但我无法看到扩展需要什么?
谢谢
想要更改html标签的对齐方式.什么都行不通.我没有在HTML中看到任何CSS.没有进一步的设置改变对齐方式.我还在UILabel上直接设置左对齐.我错过了什么?
代码是UILabel扩展名.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attr = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding), NSParagraphStyleAttributeName: paragraphStyle};
NSError *error = nil;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:attr documentAttributes:nil error:&error];
// also tried with this
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle
range:NSMakeRange(0, html.length)
];
self.attributedText = attributedText;
Run Code Online (Sandbox Code Playgroud)
编辑
好的,所以我创建了一个单独的项目,默认情况下,文本保持对齐.我必须找出为什么在上面的例子中文本显示合理.在任何情况下,主要问题仍然存在,我无法更改默认对齐方式.它在使用时初始化属性字符串时有效initWithString.但在这种情况下,HTML不起作用.当initWithData像上面的例子一样,html工作但对齐不起作用.
运行此代码段,您将看到.
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(50, 50, 300, 400);
label.backgroundColor = [UIColor yellowColor];
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentRight;
NSString …Run Code Online (Sandbox Code Playgroud) 更新1
我找到了解决办法:
// instead of
font = [font fontWithSize:pointSize];
// i can do this and get the correct font back
font = [UIFont fontWithName:font.fontName size:pointSize]
Run Code Online (Sandbox Code Playgroud)
我正在使用Nick Lockwood的HTMLLabel类将HTML转换为一个尊重HTML中样式的NSAttributedString.在大多数情况下,它很有效.
但是,我在我的应用程序中使用自定义字体,出于某种原因,我的字体总是以粗体显示.我没有使用系统字体的这个问题.我把它缩小到这个方法:
- (UIFont *)font
{
// ** Code Path 1 **
UIFont *font = _styles[HTMLFont];
if ([font isKindOfClass:[NSString class]])
{
font = [UIFont fontWithName:(NSString *)font size:17.0f];
}
NSInteger pointSize = [_styles[HTMLTextSize] floatValue] ?: font.pointSize;
if (self.bold) {
if (self.italic)
{
font = [font boldItalicFontOfSize:pointSize];
}
else
{
font = [font boldFontOfSize:pointSize]; …Run Code Online (Sandbox Code Playgroud) 使用语法有点困惑.我知道你应该使用NSFontAttributeName,但我不知道如何正确指定范围.
我很好奇两个案例.
如何指定文本中所有字符的范围,以及如何将范围指定为前10个字符.
有什么建议?
ios ×6
objective-c ×4
swift ×3
uilabel ×2
uitextview ×2
cocoa-touch ×1
core-text ×1
fonts ×1
instruments ×1
ios6 ×1
ios7 ×1
iphone ×1
multiline ×1
swift3 ×1
uifont ×1
uikit ×1