标签: nsmutableattributedstring

NSMutableAttributedString添加不同的对齐方式

是否可以在字符串的不同部分添加左对齐和右对齐?

我尝试将对齐属性添加到右侧部分:

    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrahStyle setAlignment:NSTextAlignmentRight];
    [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:rangeOfDate];
Run Code Online (Sandbox Code Playgroud)

但是整个字符串与左边对齐.

objective-c ios6 nsmutableattributedstring

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

NSMutableAttributedString initWithData:在旋转时导致EXC_BAD_ACCESS

我在a中显示不同类型的内容,tableview并使用不同的自定义方法计算每个单元格的高度heightForRowAtIndexPath.

其中一个自定义方法意味着转换一些html NSMutableAttributedString,然后计算它的高度NSMutableAttributedString.
对于html转换,我使用新initWithData:方法.

一切都很完美,除非我旋转屏幕=>我每次都有一个exc_bad_access.

使用Instruments/Zombies,我已经能够找到错误,事实上就是这样initWithData:.

(当我删除此方法并创建一个"简单" NSMutableAttributedStringinitWithString,我可以根据需要更改方向,crash不再有).

知道为什么吗?

(顺便说一句,我的项目使用ARC)


Instrument/Zombie截图: 在此输入图像描述


调用的自定义方法heightForRowAtIndexPath:

<UtilitiesForFrontEndUI heightForFacebookAttributedText:>

+(CGFloat)heightForFacebookAttributedText:(NSString *)attributedText withWidth:(CGFloat)width
{
    NSAttributedString *formatedText = [self formatRawFacebookContentForFrontEndRichTextContents:attributedText];
    CGRect rect= [formatedText boundingRectWithSize:CGSizeMake(width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return ceilf(rect.size.height);
}
Run Code Online (Sandbox Code Playgroud)

使用initWithData进行html NSMutableAttributedString转换的自定义方法:

<UtilitiesForFrontEndUI formatRawFacebookContentForFrontEndRichTextContents:>

+(NSAttributedString *)formatRawFacebookContentForFrontEndRichTextContents:(NSString *)stringToFormat
{
    // THIS GENERATE EXC_BAD_ACCESS ON DEVICE ROTATION (WORKS IF NO ROTATION)
    NSData *dataContent = [stringToFormat dataUsingEncoding:NSUTF8StringEncoding]; …
Run Code Online (Sandbox Code Playgroud)

exc-bad-access ios nsmutableattributedstring ios7

13
推荐指数
1
解决办法
7152
查看次数

在swift中为所有出现的字符串着色

这段代码

var textSearch="hi"
var textToShow="hi hihi hi" 
var rangeToColor = (textToShow as NSString).rangeOfString(textSearch)
var attributedString = NSMutableAttributedString(string:textToShow)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor() , range: rangeToColor)
TextView.attributedText=attributedString
Run Code Online (Sandbox Code Playgroud)

给我NSRange为TextView中的字符串着色.问题是我只返回第一次出现.如果单词包含"hi hihi hi",则仅第一个"hi"被着色.如何获取所有出现的字符串?

nsstring ios nsmutableattributedstring swift

13
推荐指数
2
解决办法
3730
查看次数

如何将粗体和斜体应用于NSMutableAttributedString范围?

我一直在尝试将NSFontAttributes的组合最近应用到NSMutableAttributedString,我根本找不到如何在不删除其他属性的情况下进行彻底的解释.

我搜索了一堆,发现这个问题与如何用HTML做,然后这个问题关于如何找到文本加粗或斜体的地方,但没有关于如何实际做到这一点.

目前,我尝试按如下方式格式化蜇:

斜体字: [mutableAttributedString addAttribute: NSFontAttributeName value:[fontAttributes valueForKey:CXItalicsFontAttributeName] range:r];

胆大: [mutableAttributedString addAttribute:NSFontAttributeName value:[fontAttributes valueForKey:CXBoldFontAttributeName] range:r];

其中常量CXItalicsFontAttributeNameCXBoldAttributeName从字典中提取以下两个值:

UIFont *italicsFont = [UIFont fontWithName:@"Avenir-BookOblique" size:14.0f];
UIFont *boldFont = [UIFont fontWithName:@"Avenir-Heavy" size:14.0f];
Run Code Online (Sandbox Code Playgroud)

我知道这不一定是正确的格式化方法,因为NSAttributedString 标准属性不包括ItalicsFontAttribute或BoldFontAttribute,但我找不到正确的方法来执行此操作.任何人都可以帮我吗?

objective-c nsstring ios nsmutableattributedstring

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

如何使用NSMutableAttributedString在我的UITextView中加粗一些单词?

我有一个UITextView,并且我正在使用NSString stringWithFormat投射某些单词,我想加粗.

我已经浏览了Stack Overflow并试图关注帖子,但我想我不理解它.

这是我一直在玩的东西:

    NSRange boldedRange = NSMakeRange(0, 4);
    NSString *boldFontName = [[UIFont fontWithName:@"Helvetica-Bold" size:100]fontName];


    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.name];

    [attrString beginEditing];
    [attrString addAttribute:NSFontAttributeName
                       value:boldFontName
                       range:boldedRange];
    [attrString endEditing];

    self.resultsTextView.attributedText = attrString;


    self.resultsTextView.text = [NSString stringWithFormat:@"One day, %@ was taking a walk and saw a %@ boy.  He was %@ a %@.", attrString, self.adjective, self.adverb, self.noun];
Run Code Online (Sandbox Code Playgroud)

objective-c uitextview nsattributedstring ios nsmutableattributedstring

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

突出显示UILabel中的文本

我正在尝试设置背景颜色/突出显示文本中的文本UILabel.问题是UILabel为了保持文本居中而添加的换行符和空格也被突出显示.

在此输入图像描述

注意UILabel突出显示最后一行之前的间距.此外,还突出显示任何新行的开头和结尾.

我正在使用以下代码创建上面的示例:

-(void)createSomeLabel {
    // Create and position my label
    UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                                   0,
                                                                   self.view.frame.size.width - 40,
                                                                   self.view.frame.size.height - 300)];
    someLabel.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
    someLabel.textAlignment = NSTextAlignmentCenter;
    someLabel.textColor = [UIColor whiteColor];
    someLabel.lineBreakMode = NSLineBreakByWordWrapping;
    someLabel.numberOfLines = 0;
    [self.view addSubview:someLabel];

    // This string will be different lengths all the time
    NSString *someLongString = @"Here is a really long amount of text that is going to wordwrap/line break …
Run Code Online (Sandbox Code Playgroud)

objective-c uilabel ios nsmutableattributedstring

12
推荐指数
1
解决办法
2725
查看次数

如何从NSAttributedString swift中删除属性?

我在按钮attributesTitle中添加了一些属性

 let attr = NSMutableAttributedString(string: currTitle)

 attr.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attr.length))
 attr.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0,  attr.length))

 currButton?.setAttributedTitle(attr, forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)

按钮点击后如何从中删除NSStrikethroughStyleAttributeName?

setattribute nsattributedstring ios nsmutableattributedstring swift

12
推荐指数
1
解决办法
4646
查看次数

Swift 4.2:[Swift._EmptyArrayStorage _getValue:forType:]:无法识别的选择器

NSInvalidArgumentException将项目升级到Swift 4.2(从4.0转换)后,我遇到异常.

2018-09-19 15:37:33.253482+0100 <redacted>-beta[3715:1010421] -[Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector sent to instance 0x107e6c290
2018-09-19 15:37:33.254312+0100 <redacted>-beta[3715:1010421] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector sent to instance 0x107e6c290'
Run Code Online (Sandbox Code Playgroud)

它是与a相关的东西NSMutableAttributedString和它添加一些属性的代码,例如,a NSAttributedString.Key.underlineStyle.

即将分配属性字符串时会发生异常: textView.attributedText = mutableAttributedString.


更新:

所以,它在Swift 4中工作: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.styleNone.rawValue, range: range)

然后,使用Swift 4.2,编译器建议更改NSUnderlineStyle.styleNone.rawValueNSUnderlineStyle.none.rawValue

之后,编译器开始大喊" 'none'不可用:使用[]构造一个空选项集 ":

mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.none.rawValue, range: range)
                                                                                   ^~~~~ 'none' is unavailable: use [] to construct an empty option set
Run Code Online (Sandbox Code Playgroud)

nsmutableattributedstring swift swift4.2

10
推荐指数
1
解决办法
2092
查看次数

在 SwiftUI `Text` 中使用 `NSTextAttachment`

SwiftUI 不能Text使用吗NSTextAttachment?我有这个AttributedString

var attributedString: AttributedString {
    var sampleAttributedString = AttributedString(sampleText)
    // Apply some bold and italics attributes to sampleAttributedString
     ...
    // create NSMutableAttributedString
    let mutableAttributedString = NSMutableAttributedString(sampleAttributedString)
    let image1Attachment = NSTextAttachment()
    image1Attachment.image = UIImage(named: "audio.png")
    let imageString = NSAttributedString(attachment: image1Attachment)
    mutableAttributedString.append(imageString)
    mutableAttributedString.append(NSAttributedString(string: "End of text"))

    sampleAttributedString = AttributedString(mutableAttributedString)

    return sampleAttributedString
}
Run Code Online (Sandbox Code Playgroud)

当我在 SwiftUI 视图中使用上述内容时,如下所示:

var body: some View {
    Text(attributedString)
}
Run Code Online (Sandbox Code Playgroud)

我上面嵌入的图像没有显示。所有其他文本均按预期呈现。没有catch被调用。该图像位于资产中,我也尝试过系统图像。

如果我将相同的属性字符串设置为attributedTexta 的属性,则相同的逻辑将起作用UILabel。任何帮助表示赞赏。

nsattributedstring nsmutableattributedstring swift swiftui

10
推荐指数
1
解决办法
635
查看次数

在NSMutableAttributedString中找到子串的Rang

我有像这样的"@Mervin测试员"的表情符号的AttributedString

现在我需要在这个属性String中找到一系列Mervin.

let attributedString = NSMutableAttributedString(string: " @Mervin tester ")

let range = // range for "Mervin" in above String. 
Run Code Online (Sandbox Code Playgroud)

谢谢.

nsattributedstring nsrange nsmutableattributedstring swift

9
推荐指数
1
解决办法
4118
查看次数