有没有人知道在将AppKit变成UIKit时是什么让Apple遗漏了NSAttributedString?
我问的原因是我真的想在我的iPhone应用程序中使用它,似乎没有替代或替代自己做...
可以在字符串上使用混合字体属性 - 实现与使用NSAttributedString的几行代码可能实现的类似工作只需要很多工作.
此外,自己做所有这些额外的绘图代码使我的表视图单元格真的很重,并且真的会伤害性能.
有人有任何想法吗?任何天才都在开发替代NSAttributedString的开源?
任何人都可以向我解释NSAttributedString如何正确遵循MVC范式?我知道它不是从NSString继承的,但它仍然是一个字符串,所以我会说这是我模型的一部分.但是,在谈论MVC时,设置UI属性(如下划线,字体,阴影等)显然是View的一部分,所以我不确定这是如何遵循规则的.
我想知道是否可以将UIButton放在一段文本的精确坐标上.
例如,我的UILabel有这样的文字:
"没有账号?登录 "
在斜体内容中,我需要在此处按下按钮,触摸此按钮将触发将打开登录视图的操作
非常重要的是要使这个功能很好,在其他语言中使用相同而不做任何事情:)
谢谢
我正在尝试将属性文本设置为标签.这些属性似乎是一种工作字体和颜色.
我唯一面临的问题是线条的包装.UILabel的大小为(200,300),numberofLines = 0.因此,它应该包裹线,但它不会发生.
NSMutableString *title=[[NSMutableString alloc] init];
NSRange range1;
NSRange range2;
NSRange range3;
NSString *str1=@"ABCD EFGHI klm";
[title appendString:str1];
range1=NSMakeRange(0, str1.length);
NSString *str2=@"PQRSSSS ";
[title appendString:str2];
range2=NSMakeRange(range1.length, str2.length);
NSString *str3=@"1235 2347 989034 023490234 90";
[title appendString:str3];
range3=NSMakeRange(range2.location+range2.length, str3.length);
NSMutableAttributedString *attributeText=[[NSMutableAttributedString alloc] initWithString:title];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color1,NSForegroundColorAttributeName,[self getStlylishItalicFont:13.0] ,NSFontAttributeName,nil] range:range1];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color2,NSForegroundColorAttributeName,[self getStylishFont:13.0] ,NSFontAttributeName,nil] range:range2];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color3,NSForegroundColorAttributeName,[self getStylishBoldFont:13.0] ,NSFontAttributeName,nil] range:range3];
self.myTextLabel.attributedText=attributeText;
Run Code Online (Sandbox Code Playgroud)
UILabel显示如下,即使高度为300.
ABCD EFGHI klm PQRSSSS 1235 234 ...
我的代码已经有了这个属性,但是如何为下划线添加属性?
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName, foregroundColor, NSForegroundColorAttributeName, nil];
Run Code Online (Sandbox Code Playgroud) 我想用2种颜色创建一个uilabel.第一种颜色是黑色,另一种颜色是蓝色.
我可以在其中使用多个uilabel,但我想只有一个uilabel.有什么办法可以实现吗?
这应该是输出.

这是我的代码:
UILabel * lblPostContent = [[UILabel alloc] initWithFrame:CGRectMake((ICON_PADDING*1.5), 42, container.frame.size.width-30, 34)];
lblPostContent.numberOfLines =0;
[lblPostContent setFont:[UIFont systemFontOfSize:11]];
[lblPostContent setText:[NSString stringWithFormat:@"I just scored %d points at %@ using the iBowl app", score, LuckyStrikes]];
[container addSubview:lblPostContent];
Run Code Online (Sandbox Code Playgroud) 我的目标是在Parse.com中存储属性字符串的信息.我决定为我的图像提供一个属性文本的编码,通过用{X}相应的图像替换大括号中的任何字符串来工作.例如:
Picture of 2 colorless mana: {X}
Run Code Online (Sandbox Code Playgroud)
应该生成一个{X}由图像替换的属性字符串.这就是我尝试过的:
NSString *formattedText = @"This will cost {2}{PW}{PW} to cast.";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\\{)[^}]+(?=\\})" options:NSRegularExpressionAnchorsMatchLines
error:nil];
NSArray *matches = [regex matchesInString:formattedText
options:kNilOptions
range:NSMakeRange(0, formattedText.length)];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:formattedText];
for (NSTextCheckingResult *result in matches)
{
NSString *match = [formattedText substringWithRange:result.range];
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = [UIImage imageNamed:[NSString stringWithFormat:@"Mana%@.png", match]];
NSAttributedString *replacementForTemplate = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attributedString replaceCharactersInRange:result.range
withAttributedString:replacementForTemplate];
}
[_textView setAttributedText:attributedString];
Run Code Online (Sandbox Code Playgroud)
目前这种方法存在两个问题:
objective-c nsattributedstring nsregularexpression nsmutableattributedstring
我试图使用NSMutableAttributedString与下面的虚线下划线是我的代码,但没有一个模式似乎工作,我错过了什么?
var str : NSMutableAttributedString = NSMutableAttributedString(string: "HelloWorld")
str.addAttribute(NSUnderlineStyleAttributeName , value: NSNumber(integer:(NSUnderlineStyle.PatternDot).rawValue), range: NSMakeRange(0, str.length))
Run Code Online (Sandbox Code Playgroud) 我目前正在使用此答案提供的此扩展程序将html转换为字符串中的字符串UITextView.一切都很完美,但由于某些奇怪的原因,NSFontAttributeName在此过程中不会应用于字符串.我有什么问题吗?(或者我应该在使用NSAttributedString后面的html解析字符串后应用?如果是这样,是否可以将属性应用于NSAttributeString?).
PS.使用时字体大小不会改变,html2String但html中的链接会被更长时间识别UITextView
extension String {
var html2AttributedString: NSAttributedString? {
guard
let data = dataUsingEncoding(NSUTF8StringEncoding)
else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding, NSFontAttributeName : UIFont.systemFontOfSize(17.0)], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
cell.desc.attributedText = apiData[currentIndex].description!.html2AttributedString //Font attribute not recognized
cell.desc.text = apiData[currentIndex].description!.html2String //Font recognized by …Run Code Online (Sandbox Code Playgroud) 当我点击加粗按钮时,我有一个文本编辑器
我可以用粗体字写这是代码
txtEditor.typingAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.black/, NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: (txtEditor.font?.pointSize)!)]
Run Code Online (Sandbox Code Playgroud)
我想在“中心文本中心对齐”中单击一个按钮(我写的行)时需要一个按钮,而不是其他行
这是代码
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
Run Code Online (Sandbox Code Playgroud)
但是在将如何在UITextview中添加这2行代码以输入居中对齐之后呢?