标签: nsattributedstring

更改NSAttributedString中子字符串的属性

这个问题可能是重复的这一个.但答案对我不起作用,我希望更具体.

我有一个NSString,但我需要一个NS(Mutable)AttributedString和这个字符串中的一些单词应该给出不同的颜色.我试过这个:

NSString *text = @"This is the text and i want to replace something";

NSDictionary *attributes = @ {NSForegroundColorAttributeName : [UIColor redColor]};
NSMutableAttributedString *subString = [[NSMutableAttributedString alloc] initWithString:@"AND" attributes:attributes];

NSMutableAttributedString *newText = [[NSMutableAttributedString alloc] initWithString:text];

newText = [[newText mutableString] stringByReplacingOccurrencesOfString:@"and" withString:[subString mutableString]];
Run Code Online (Sandbox Code Playgroud)

"和"应该是大写的红色.

文档说mutableString保留属性映射.但是对于我的替换事物,我在赋值的右侧(在我的代码片段的最后一行)中没有更多的claimString.

我怎样才能得到我想要的东西?;)

replace objective-c nsattributedstring ios

17
推荐指数
2
解决办法
3万
查看次数

NSAttributedString将样式更改为粗体而不更改pointSize?

NSAttributedString在iOS上挖掘s.我有一个模型,正在返回一个人的名字和姓氏NSAttributesString.(我不知道在模型中处理属性字符串是否是一个好主意!?)我希望第一个名字要经常打印,因为姓氏应该用粗体打印.我不想要的是设置文本大小.到目前为止,我发现的只有:

- (NSAttributedString*)attributedName {
    NSMutableAttributedString* name = [[NSMutableAttributedString alloc] initWithString:self.name];
    [name setAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]} range:[self.name rangeOfString:self.lastname]];
    return name;
}
Run Code Online (Sandbox Code Playgroud)

但是,这当然会覆盖姓氏的字体大小,这样UITableViewCell可以在单元格标签的常规文本大小中打印出名字的位置非常有趣,并且姓氏将打印得非常小.

有没有办法实现我想要的?

谢谢你的帮助!

iphone nsattributedstring uifont ipad ios

16
推荐指数
3
解决办法
2万
查看次数

如何从CGSIZE获取NSAttributedString用于UITextView?

我正在使用UITextView和创建一个阅读器应用程序NSAttributedString,我需要将整个attributedString分成少量的属性字符串以启用Pages Concept.

我有计算给定属性字符串的帧大小的方法.

        CGRect rect = [attrString boundingRectWithSize:CGSizeMake(768, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
Run Code Online (Sandbox Code Playgroud)

但我需要获取(768,1024)ContentSize的attributeString.

我已经使用了这个,但是attributesStrings没有正确划分,因为它包含NSTextAttachmentHTML蜇.

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)delegate.attributedString);
Run Code Online (Sandbox Code Playgroud)

我该如何计算呢?它需要很快,需要占用少量内存.

objective-c uitextview nsattributedstring ios

16
推荐指数
1
解决办法
769
查看次数

当文本设置为"归属"时,UITextView自定义字体不起作用

我有UITextView一些来自.rtf的文本(直接粘贴到Xcode上)

上下文只包含一种自定义字体(Futura Book BT 11.0)

如果我将"text(attribution)"属性设置为"plain"=自定义字体从故事板和应用程序中正确显示

如果我将"text"属性设置为"attribution"=.自定义字体从故事板正确显示但不是从应用程序.

由于我的目标是使用多种字体工作的文本,如何使属性属性与自定义字体一起使用?(迅速)

谢谢!

xcode nsattributedstring custom-font swift

16
推荐指数
3
解决办法
8808
查看次数

NSAttributedString'\n'被忽略

我有一个视图,其唯一的UI元素是UITextView.在viewDidLoad:我创建一个带有"Text \n"的属性字符串,并将文本视图的属性文本设置为:

NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Text\n"];  
[self.textView setAttributedText:string];
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我运行应用程序时,会忽略换行符.

如果我使用了NSString,setText:那就不会发生.

NSString *string = [[NSString alloc] initWithString:@"Text\n"];  
[self.textView setText:string];
Run Code Online (Sandbox Code Playgroud)

任何人都可以了解正在发生的事情吗?我似乎无法在文档或其他方面找到任何内容.

uitextview nsattributedstring end-of-line ios ios6

15
推荐指数
1
解决办法
7600
查看次数

在UILabel中,是否可以强制线不要在某个地方中断

我有一个UILabel应该是两行长.该文本已本地化为法语和英语.

我正在设置标签的attributedText属性.该文本由三个附加的字符串构成,例如textFragment1,textFragment2和textFragment3.中间字符串实际上是使用NSTextAttachment创建的图像.

我想确保textFragment2和textFragment3在同一行.第一个字符串可能会或可能不会换行,具体取决于它的长度.问题是我的法语文本目前相当短,所以该行在textFragment2之后换行.

我通过在textFragment1的本地化法语文本中添加换行符来暂时修复此问题.我不喜欢这个解决方案.我想知道是否有办法处理textFragment2和textFragment3,以便它们将始终在同一行上.

nsattributedstring uilabel nstextattachment ios ios7

15
推荐指数
1
解决办法
3782
查看次数

UITextView linkTextAttributes字体属性未应用于NSAttributedString

我有一个NSAttributedStringHTML生成,其中包含一些链接.属性字符串显示在UITextView中.我希望为链接应用不同的字体样式,并linkTextAttributes为此设置.我添加了NSForegroundColorAttributeName,NSFontAttributeName并且NSUnderlineStyleAttributeName.由于某种原因,应用了前景色,但其余属性则没有.

myTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.redColor(), NSFontAttributeName : textLinkFont, NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleNone.rawValue]
Run Code Online (Sandbox Code Playgroud)

有没有其他人面对这个,如何更改链接的字体样式,而不必将内联CSS应用于原始HTML?谢谢.

uitextview nsattributedstring swift

15
推荐指数
4
解决办法
1万
查看次数

Swift 3 NSAttributedString多个属性

刚开始swift 3我遇到了快速语法问题.

我想要显示一个简单的NSAttributedString.

所以我先设定我的属性:

let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName : UIFont.fontSaySomething()]
let attributeColorSaySomething : [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.blue]
Run Code Online (Sandbox Code Playgroud)

然后我创建我的字符串:

let attStringSaySomething = NSAttributedString(string: "Say something", attributes: self.attributeFontSaySomething)
Run Code Online (Sandbox Code Playgroud)

我想要做的是创建具有我的2个属性的字符串,而不仅仅是一个.但当我这样做时:

let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [self.attributeFontSaySomething, self.attributeColorSaySomething])
Run Code Online (Sandbox Code Playgroud)

Xcode告诉我,我不能并希望我为字典词典更改此内容.

如何在不使用NSMutableAttributedString?的情况下使用2个属性创建字符串?

nsattributedstring ios

15
推荐指数
2
解决办法
3万
查看次数

用属性字符串替换UITextViews文本

我有一个UITextView,当用户输入文本时,我想动态格式化文本.语法高亮......

为此,我想用UITextView...

一切正常,期待一个问题:我从文本视图中获取文本并从中NSAttributedString获取.我对这个属性字符串进行了一些编辑,并将其设置为textView.attributedText.

每次用户输入时都会发生这种情况.所以我必须记住selectedTextRange编辑之前的attributedText事情,然后将其设置回来,以便用户可以继续在他之前输入的地方打字.唯一的问题是,一旦文本足够长就需要滚动,UITextView如果我慢慢输入,现在将开始滚动到顶部.

以下是一些示例代码:

- (void)formatTextInTextView:(UITextView *)textView
{
  NSRange selectedRange = textView.selectedRange;
  NSString *text = textView.text;

  // This will give me an attributedString with the base text-style
  NSMutableAttributedString *attributedString = [self attributedStringFromString:text];

  NSError *error = nil;
  NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
  NSArray *matches = [regex matchesInString:text
                                    options:0
                                      range:NSMakeRange(0, text.length)];

  for (NSTextCheckingResult *match in matches)
  {
    NSRange matchRange = [match rangeAtIndex:0];
    [attributedString addAttribute:NSForegroundColorAttributeName
                             value:[UIColor …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring core-text ios

14
推荐指数
1
解决办法
3万
查看次数

如何使NSStringDrawingContext缩小文本?

我正在尝试使用iOS 6的属性字符串API来计算文本的大小,并在必要时缩小字体大小.但是,正如文档所说,我无法让它工作.

NSString *string = @"This is a long text that doesn't shrink as it should";

NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = 0.5;

UIFont *font = [UIFont fontWithName:@"SourceSansPro-Bold" size:32.f];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                              NSParagraphStyleAttributeName: paragraphStyle };

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.title attributes:attributes];

CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(512.f, 512.f) options:NSStringDrawingUsesLineFragmentOrigin context:context];

NSLog(@"rect: %@, context: %@", NSStringFromCGRect(rect), context.debugDescription);
Run Code Online (Sandbox Code Playgroud)

但是文本没有缩小并被截断.actualScaleFactor永远1.日志结果如下:

rect:{{0, 0}, {431.64801, 80.447998}}, context:<NSStringDrawingContext: 0x14e85770> minimumScaleFactor:0.500000 …
Run Code Online (Sandbox Code Playgroud)

text nsattributedstring ios

14
推荐指数
2
解决办法
5617
查看次数