标签: nsattributedstring

使用NSAttributedString为UILabel设置突出显示的文本颜色.

我在tableview单元格上使用标签.已为单元格分配了一个属性字符串,但问题是在选择时,单元格文本对于属性字符串不会变为白色.

有没有办法解决它..?

任何帮助表示赞赏.

这是我的代码.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    LHSearchViewCell *cell = [LHSearchViewCell cellForTableView:tableView fromNib:_cellNib];

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    cell.headerLabel.textColor = [UIColor darkGrayColor];
    cell.headerLabel.highlightedTextColor = [UIColor whiteColor];
    cell.headerLabel.font = [UIFont systemFontOfSize:14];

    cell.subLabel.font = [UIFont systemFontOfSize:12];
    cell.subLabel.textColor = [UIColor darkGrayColor];
    cell.subLabel.numberOfLines = 2;
    switch (indexPath.section) {
        case 0:
        {
            UIFont *font = [UIFont systemFontOfSize:14.0];
            NSDictionary *firstAttributes = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,[UIColor darkGrayColor],NSForegroundColorAttributeName,nil];
            UIFont *secondFont = [UIFont systemFontOfSize:10.0];
            NSDictionary *secondAttributes = [NSDictionary dictionaryWithObjectsAndKeys:secondFont,NSFontAttributeName,[UIColor lightGrayColor],NSForegroundColorAttributeName,nil];
            NSString* completeString = [NSString …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsattributedstring ios

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

将NSAttributedString与不同大小的字符垂直对齐

我一直在谷歌上搜索一段时间,我仍然对iOS6感到惊讶,没有办法垂直对齐NSAttributedString不同大小的字符?特别是我试图将它作为UIButton标题属性垂直对齐,这是可行的吗?

nsattributedstring ios

5
推荐指数
0
解决办法
1073
查看次数

寻求的解决方法:iOS 6 NSAttributedString下行器阻止下划线

我正在使用NSAttributedString(和NSMutableAttributeString)通过写入PDF UIGraphicsBeginPDFContextToFile.当我输出带有下划线属性的属性字符串时,当字母下行器超过下划线时,下划线会被破坏.

以下是显示当前输出的示例(屏幕截图):

样本下划线图像

以下是构建该示例属性字符串的代码:

NSAttributedString* ftype = 
       [[NSMutableAttributedString alloc] 
                    initWithString:@"Dangerous"
                        attributes:@{ 
                                   NSParagraphStyleAttributeName:pstyle,
                                   NSFontAttributeName:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:48.0], 
                                   NSForegroundColorAttributeName:[UIColor redColor],
                                   NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]
                                   }
       ];
Run Code Online (Sandbox Code Playgroud)

我的客户和我的偏好是下划线是连续的,理想情况下转移到下降器下方.

手动绘制下划线很困难,因为我们必须计算布局后单词的文本位置(有时文本不像上面的例子那么简单).

有没有人有一个修复,以下划线a)降低或b)使其连续?

提前致谢.

iphone nsattributedstring ios

5
推荐指数
1
解决办法
608
查看次数

UILabel与NSAttributedString忽略换行符

我有一个UILabel,行数设置为0,它使用尾部截断.标签大小足以容纳2行文本.如果我给它一个常规的NSString,其中包含3行文本,其中每行都很短并且在\n处有\n,它将显示前2行然后是椭圆.它看起来像:1号线2号线......

但是如果我使用具有相同文本的NSAttributedString,它会在截断结果之前组合第2行和第3行.它看起来像:Line 1 Line 2Line 3

我没有尝试过使用CoreText并逐行修复它似乎能够让它正常工作.

有什么建议?

nsattributedstring uilabel ios

5
推荐指数
1
解决办法
617
查看次数

垂直对齐UILabel中的属性文本

设置UILabel的属性文本并使用setFontSizeToFitWidth时,属性文本字体大小按预期调整大小.但是,当属性字符串字体大小调整为较小的字体大小时,文本在UILabel内部不会垂直对齐.

我需要使用adjustFontSizeToFitWidth方法,因为属性字符串具有可变大小.我的最小字体大小设置为"15.0",最大字体大小设置为"28.0".因此我使用minimumScaleFactor"15.0/28.0"

我的代码:

NSAttributedString *balance = [[NSAttributedString alloc] initWithString:@"12390765298374652938756" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

// Create UILabel with a 10.0 point padding around the UILabel within the parent Rect
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 10, self.bounds.origin.y + 10, self.bounds.size.width - 20, self.bounds.size.height - 20)];

textLabel.attributedText = currencyWithBalance;
textLabel.font = [UIFont fontWithName:@"TitilliumWeb-Light" size:28.0];
textLabel.minimumScaleFactor = 15.0/28.0;
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.numberOfLines = 1;
textLabel.backgroundColor = [UIColor redColor];


[self addSubview:textLabel];
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我实现这一目标,以便文本也垂直对齐吗?

多谢你们.

objective-c vertical-alignment nsattributedstring uilabel ios

5
推荐指数
1
解决办法
6454
查看次数

将带有图像的NSAttributedString保存到RTF文件时遇到麻烦

我有一些非常简单的RTF文件输出。当我生成此文档时,用户可以通过电子邮件发送该文档。所有这些都很好。该文档看起来不错。一旦有了NSAttributedString,就制作一个NSData块,并将其写入文件,如下所示:

NSData* rtfData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} error:&error];
Run Code Online (Sandbox Code Playgroud)

该文件可以通过电子邮件发送。当我检查电子邮件时,一切都很好。

现在,我的任务是在文档顶部添加UIImage。太好了,所以我正在创建这样的属性字符串:

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];

UIImage* image = [UIImage imageNamed:@"logo"];
attachment.image = image;
attachment.bounds = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);

NSMutableAttributedString *imageAttrString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];

// sets the paragraph styling of the text attachment

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ;

[paragraphStyle setAlignment:NSTextAlignmentCenter];            // centers image horizontally

[paragraphStyle setParagraphSpacing:10.0f];   // adds some padding between the image and the following section

[imageAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [imageAttrString length])]; …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring nstextattachment ios ios7

5
推荐指数
2
解决办法
3497
查看次数

粘贴格式化的文本,而不是图像或HTML

我正在尝试模拟iOS Pages和Keynote应用程序的粘贴板行为。简而言之,允许将基本NSAttributedString文本格式(即BIU)粘贴到UITextView中,但不能粘贴到图像,HTML等中。

行为摘要

  1. 如果从Notes应用程序,Evernote复制格式化的文本,或从网站复制文本和图像,则Pages仅粘贴纯文本字符串
  2. 如果您从Pages或Keynote中复制格式化的文本,则会将格式化的文本粘贴到Pages,Keynote等中的其他位置。
  3. 一个不希望的结果,但也许很重要的一点是,Notes应用程序或Evernote都不会粘贴从Pages或Keynote复制的格式化文本。我推测应用之间的差异在于使用NSAttributedStrings而不是HTML?

这是如何完成的?在Mac OS上,您似乎可以要求粘贴板返回不同类型的自身,让其提供富文本格式和字符串表示形式,并首选使用富文本格式。不幸的是,iOS似乎不存在readObjectsForClasses。就是说,由于这篇文章,我可以通过日志看到iOS确实具有与RTF相关的粘贴板类型。但是,我找不到一种方法来请求粘贴板内容的NSAttributedString版本,以便可以优先进行粘贴。

背景

我有一个应用程序,允许UITextViews中的文本的基本NSAttributedString用户可编辑格式(即,粗体,斜体,下划线)。用户想要从其他应用程序复制文本(例如Safari中的网页,Notes应用程序中的文本),以粘贴到我的应用程序中的UITextView中。允许剪贴板默认运行,意味着我可能会得到应用程序不打算处理的背景颜色,图像,字体等。下面的示例显示了文本,当粘贴到我的应用程序的UITextView中时,具有背景色的复制文本看起来如何。

在此处输入图片说明

我可以通过继承UITextView来克服1

- (void)paste:(id)sender
{
    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
    NSString *string = pasteBoard.string;
    NSLog(@"Pasteboard string: %@", string);
    [self insertText:string];
}
Run Code Online (Sandbox Code Playgroud)

意外的结果是,失去了保留从我的应用程序中复制的文本格式的功能。用户可能想要从我的应用程序中的一个UITextView复制文本,然后将其粘贴到我的应用程序中的另一个UITextView。他们希望保留格式(即粗体,斜体,下划线)。

的见解和建议表示赞赏。

copy-paste nsattributedstring uipasteboard ios ios7

5
推荐指数
1
解决办法
1374
查看次数

带有NSAttributedString的简单UIAlertView

我正在寻找一种简单的方法来使用NSAttributedString一个非常简单的消息框类似于:

NSString *new_item = [NSString stringWithFormat:@"<span style=\"font-family: Helvetica Neue; font-size: 12.0\">%@</span>", @"MOTD HTML String downloaded from website"];
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[new_item dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MOTD"
                                                   message:attrStr
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
[alert show];
Run Code Online (Sandbox Code Playgroud)

我上面的代码采用从服务器下载的HTML格式的字符串,确保文本大小适合屏幕,然后尝试发送NSAttributedString到UIAlertView.但UIAlertView不喜欢这个.这个问题最简单的方法是什么?(非HTML格式的MOTD不是一个选项)

objective-c nsattributedstring uialertview ios

5
推荐指数
2
解决办法
9265
查看次数

使用旧金山字体将HTML解析为NSAttributedString?(iOS 9)

我需要解析包含<b>(粗体),<i>(斜体)和<u>(下划线)标签的基本HTML字符串,没有别的,只是那些简单的标签.

现在,我只能得到<u>(下划线)标签在正确渲染NSAttributedString,使用新的时San Francisco中iOS 9.

我真的需要得到<b>(粗体)和<i>(斜体)渲染.

这是我正在做的事情:

let text = "<i>Any</i> <b>Text</b> <u>that's basic HTML</u>"
let font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)

let modifiedFont = NSString(format:"<span style=\"font-family: '-apple-system','HelveticaNeue'; font-size: %f \">%@</span>",
font.pointSize, text) as String

let data = (modifiedFont as NSString).dataUsingEncoding(NSUTF8StringEncoding)

let attributedString = try? NSAttributedString(data: data!, options:
            [ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                NSCharacterEncodingDocumentAttribute : NSUTF8StringEncoding,
                NSFontAttributeName : font ],
            documentAttributes: nil)
Run Code Online (Sandbox Code Playgroud)

但不幸的是,<i>(斜体)和<b>(粗体)标签永远不会渲染,只有 …

accessibility nsattributedstring uifont ios ios9

5
推荐指数
1
解决办法
2116
查看次数

在3D触摸UITextView中的链接时自定义预览/提交视图控制器

我有一个UITextView显示NSAttributedString包含各种链接(via NSLinkAttributeName)的东西,我希望能够使用我自己的视图控制器作为预览和提交视图控制器,而不是默认行为.

我认为这是可能的,因为用于的UITextItemInteraction枚举UITextViewDelegate包含3D触摸的情况.

我试过了:

  • 寻找一个解决方案,UITextViewDelegate但最接近我想要的功能,textView(_:shouldInteractWith:in:interaction:)似乎并没有让你改变预览或提交视图控制器 - 只是禁用默认行为

  • 这样做的方式,你会与任何其他视图并注册它的3D触摸和使用previewingContext(_:viewControllerForLocation:)从UIViewControllerPreviewingDelegate,但我不能找到一个办法让已经触及3D链接

有人可以让我知道正确的方法吗?

uitextview nsattributedstring ios 3dtouch

5
推荐指数
0
解决办法
113
查看次数