基于对此问题的接受答案,我编写了以下代码:
NSData* somedata;
somedata=[NSKeyedArchiver archivedDataWithRootObject:ts];
Run Code Online (Sandbox Code Playgroud)
其中ts是一个NSAttributedString,它填充了一些文本和一些属性(在本例中为颜色).
当我执行此代码时,我收到此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x6eb5b90'
Run Code Online (Sandbox Code Playgroud)
我是NSCoder竞技场的新手,但上述问题的答案使我觉得这就是我所要做的.是吗?我错过了什么?
编辑:
在这种情况下,无法识别的选择器被发送到NSAttributedString中的颜色属性.当我像这样初始化字符串时:
NSAttributedString *ts = [[NSAttributedString alloc] initWithString:text attributes:self.currentAttributeDictionary];
Run Code Online (Sandbox Code Playgroud)
字典就像这样构建:
self.currentAttributeDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
[self.currentColor CGColor],(NSString*)kCTForegroundColorAttributeName,
nil];
Run Code Online (Sandbox Code Playgroud)
字典的NSLog产生了这个:
New dictionary is: {
CTForegroundColor = "<CGColor 0x6eb5b90> [<CGColorSpace 0x6e968c0> (kCGColorSpaceDeviceRGB)] ( 1 1 0 1 )";}
Run Code Online (Sandbox Code Playgroud)
上面的CGColor的地址与错误消息中的地址匹配.
我试图动态更改NSAttributedString的字体大小.问题是字符串包含不同的字体大小和属性.因此,当我更改字体大小时,所有内容大小都会更改为tat值.没有相应改变... .
如何NSAttributedString使用ranged属性初始化?
就目前而言,我只能弄清楚如何在初始化后添加一个ranged属性,这显然不适用于不可变NSAttributedString实例.
如果我有NSMutableAttributedString,我可以打电话:
[str addAttribute:NSLinkAttributeName value:url range:range];
Run Code Online (Sandbox Code Playgroud)
如果我有NSAttributedString,我可以构建它:
[[NSAttributedString alloc] initWithString:str attributes:@{NSLinkAttributeName: url}];
Run Code Online (Sandbox Code Playgroud)
但我找不到将范围放入attributesDict的方法.
谢谢,
有没有办法可以围绕每个单词绘制边框UILabel.假设UILabel包含字符串"This is the Line 1".
我想要5个不同的边框,大约5个字
- 这个
- 是
- 该
- 线
- 1
我一直面临一个大问题,可能整天都有一个简单的解决方案.我有一个UILabel,它包含一个NSAttributedString.我想要一个特定的范围UILabel来进行点播.我不想要任何第三方功能.
这是我的示例代码:
let str = dict["itemName"] as! NSString
range = string.rangeOfString(dict["itemName"] as! String)!
index = string.startIndex.distanceTo(range.startIndex)
attributedString.addAttribute(NSForegroundColorAttributeName, value: color, range: NSRange(location: index, length: str.length))
Run Code Online (Sandbox Code Playgroud) 码:
txtField.attributedPlaceholder = NSAttributedString(string:strPlaceHolder, attributes:[NSForegroundColorAttributeName:UIColorFromRGB(color:PlaceHolderColor, alpha:1.0),NSFontAttributeName :UIFont(name: "Didot-Italic", size: 4)!])
Run Code Online (Sandbox Code Playgroud)
我试图更改UITextField占位符字体类型和字体大小.它不起作用.我的代码出了什么问题?任何帮助将不胜感激.
我有一个UITextView带有属性的文本,其设置如下:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is a message.\nClick here for more info"];
textView.linkTextAttributes = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
NSRange linkRange = [attributedString.string rangeOfString:@"Click here for more info"];
[attributedString addAttribute:NSLinkAttributeName value:@"" range:linkRange];
textView.attributedText = attributedString;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(infoTapped:)];
[textView addGestureRecognizer:tapRecognizer
Run Code Online (Sandbox Code Playgroud)
然后我抓住了这样的水龙头:
- (void)infoTapped:(UITapGestureRecognizer *)tapGesture {
if (tapGesture.state != UIGestureRecognizerStateEnded) {
return;
}
UITextView *textView = (UITextView *)tapGesture.view;
CGPoint tapLocation = [tapGesture locationInView:textView];
UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
NSString …Run Code Online (Sandbox Code Playgroud) 我已经做了一个自定义的UIViewController调用ViewControllerA并希望能够使用它,所以我做了一个UIViewControllerRepresentable调用, ViewControllerARepresentable如下所示,但问题是,当我ViewControllerARepresentable在 SwiftUI 视图中调用并传递一个值时stringToUpdateTextView,ViewControllerA该htmlTextView(UITextView)inViewControllerA是nil,我是不知道为什么。
ViewControllerARepresentable(stringToUpdateTextView: "<html>Send some Html Text as string here</html>")
Run Code Online (Sandbox Code Playgroud)
ViewControllerA可表示
public struct ViewControllerARepresentable: UIViewControllerRepresentable {
var stringToUpdateTextView: String
public func makeUIViewController(context: Context) -> ViewControllerA {
let viewcontrollerA = ViewControllerA(testString: testingString)
return viewcontrollerA
}
public func updateUIViewController(_ uiViewController: ViewControllerA, context: Context) {}
}
Run Code Online (Sandbox Code Playgroud)
视图控制器A
open class ViewControllerA: UIViewController {
public var stringToUpdateTextView: String
override open func viewDidLoad() {
super.viewDidLoad() …Run Code Online (Sandbox Code Playgroud) 我尝试将一些属性设置为 NSAttributedString。除了前景色外,一切正常。
这就是我尝试设置属性的方式:
func setLabelText(text:String){
let strokeTextAttributes = [
NSAttributedString.Key.strokeColor : UIColor.white,
NSAttributedString.Key.foregroundColor : UIColor.red,
NSAttributedString.Key.font : UIFont.init(name: "Raleway-ExtraBold", size: 26)!,
NSAttributedString.Key.strokeWidth : 0.5,
]
as [NSAttributedString.Key : Any]
label.attributedText = NSMutableAttributedString(string: text, attributes: strokeTextAttributes)
}
Run Code Online (Sandbox Code Playgroud)
正如您在图像中看到的,它没有设置文本颜色:
你知道为什么它会忽略foregroundColor属性吗?
提前致谢。
AttributedString我尝试使用SwiftUI 3 中的新功能
我想改变对齐方式,但它不起作用。其他任何事情都工作正常,例如我尝试使用属性字符串更改颜色,它确实有效!但我无能为力paragraphStyle
struct ContentView: View {
var body: some View {
ZStack {
Color(uiColor: UIColor(red: 0.92, green: 0.92, blue: 0.92, alpha: 1.00)).ignoresSafeArea()
VStack {
Text("""
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but …Run Code Online (Sandbox Code Playgroud) 我在label上添加了属性字符串.我显示的html文本工作正常,但默认情况下它始终显示Times新罗马家族.请告诉我如何更改文本系列.
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud) ios ×7
swift ×5
objective-c ×4
swiftui ×2
uilabel ×2
ios7 ×1
ios9 ×1
nscoder ×1
nsrange ×1
uifont ×1
uitextfield ×1
uitextview ×1
uiviewcontrollerrepresentable ×1
xcode5 ×1