从textField中选择粗体和斜体文本 [英] Select Bold and Italicized text from textField

查看:148
本文介绍了从textField中选择粗体和斜体文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何只选择用户在textField / textView中输入的粗体斜体文本?



我们可以选择文字粗体,斜体,下划线以及这三者的任意组合,反之亦然。



* 这不是特定于Mac OSX或iOS的,任何一个解决方案对我都有好处。



p>

我尝试通过读取属性字符串中的文本作为:

  NSAttributedString * string = self.textView.string; 

但是由于textView和textField返回 NSString 所有格式化都消失了。

解决方案

在iOS上使用attributText属性与标签/ textfields



在OSX上使用attributStringValue



,您可以通过attributText的属性枚举并检查每个属性。生成一些代码(osx& iOS)

  NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@none ]; 

id temp = [[NSAttributedString alloc] initWithString:@boldattributes:@ {NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}];
[str appendAttributedString:temp];

temp = [[NSAttributedString alloc] initWithString:@italic属性:@ {NSFontAttributeName:[UIFont italicSystemFontOfSize:12]}];
[str appendAttributedString:temp];

temp = [[NSAttributedString alloc] initWithString:@noneattributes:@ {NSFontAttributeName:[UIFont systemFontOfSize:12]}];
[str appendAttributedString:temp];

temp = [[NSAttributedString alloc] initWithString:@bold2attributes:@ {NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}];
[str appendAttributedString:temp];

self.label.attributedText = str;

NSMutableString * italics = [NSMutableString string];
NSMutableString * bolds = [NSMutableString string];
NSMutableString * normals = [NSMutableString string];

for(int i = 0; i //可以被调整:通过考虑有效范围并且不检查每1 $ b的1 $ b // warn:==现在可能工作,但也许我会更冷,以检查字体traits使用CoreText
UIFont * font = [str attribute:NSFontAttributeName atIndex:i effectiveRange:nil];
if(font == [UIFont italicSystemFontOfSize:12]){
[italics appendString:[[str mutableString] substringWithRange:NSMakeRange(i,1)]];
} else if(font == [UIFont boldSystemFontOfSize:12]){
[bolds appendString:[[str mutableString] substringWithRange:NSMakeRange(i,1)]];
} else {
[normals appendString:[[str mutableString] substringWithRange:NSMakeRange(i,1)]];
}
}

NSLog(@%@,italics);
NSLog(@%@,bolds);
NSLog(@%@,normals);

现在这里是如何找到它。从这里选择范围应该很简单:)



注意:你只能有一个连续的选择!在osx和ios上都不能选择textfield / textview的n个部分


How can I select only bold and italicized text entered by user in a textField/textView ?

We can make a selected text bold, italicized, underline and any combination of these three but what about its vice-versa.

*This is not specific to Mac OSX or iOS, solution in either one is good for me.

EDIT:

I tried by reading the text in attributed string as :

NSAttributedString *string=self.textView.string;

But as textView and textField returns NSString so all formatting is gone.

解决方案

on iOS use attributedText properties with labels/textfields

on OSX use attributedStringValue

you can then enumerate through the attributedText's attributes and check each attribute. Ill whip up some code (osx & iOS)

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"none "];

id temp = [[NSAttributedString alloc] initWithString:@"bold " attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:12]}];
[str appendAttributedString:temp];

temp = [[NSAttributedString alloc] initWithString:@"italic " attributes:@{NSFontAttributeName: [UIFont italicSystemFontOfSize:12]}];
[str appendAttributedString:temp];

temp = [[NSAttributedString alloc] initWithString:@"none " attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]}];
[str appendAttributedString:temp];

temp = [[NSAttributedString alloc] initWithString:@"bold2 " attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:12]}];
[str appendAttributedString:temp];

self.label.attributedText = str;

NSMutableString *italics = [NSMutableString string];
NSMutableString *bolds = [NSMutableString string];
NSMutableString *normals = [NSMutableString string];

for (int i=0; i<str.length; i++) {
    //could be tuned: MOSTLY by taking into account the effective range and not checking 1 per 1
    //warn: == might work now but maybe i'd be cooler to check font traits using CoreText
    UIFont *font = [str attribute:NSFontAttributeName atIndex:i effectiveRange:nil];
    if(font == [UIFont italicSystemFontOfSize:12]) {
        [italics appendString:[[str mutableString] substringWithRange:NSMakeRange(i, 1)]];
    } else if(font == [UIFont boldSystemFontOfSize:12]){
        [bolds appendString:[[str mutableString] substringWithRange:NSMakeRange(i, 1)]];
    } else {
        [normals appendString:[[str mutableString] substringWithRange:NSMakeRange(i, 1)]];
    }
}

NSLog(@"%@", italics);
NSLog(@"%@", bolds);
NSLog(@"%@", normals);

Now here is how to find it. Deducing a selection range from this should be easy peasy :)

Note: you can only have a continuous selection! neither on osx nor on ios can you select n parts of a textfield/textview

这篇关于从textField中选择粗体和斜体文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆