标签: nsattributedstring

appendAttributedString:在NSMutableAttributedString中

我有一个MSMutableAttributedString displayContent.内容的属性在字符串中有所不同,即颜色和字体大小可能因字母而异.

我想在字符串的末尾添加一个新字符,并让它获取displayContent中最后一个字符的属性.由于用户无法控制,我无法预先知道这些属性是什么.

当我追加新角色(tempAttr)时:

NSAttributedString * tempAttr = [[NSAttributedString alloc] initWithString:appendage];
[displayContent appendAttributedString:tempAttr];
Run Code Online (Sandbox Code Playgroud)

它似乎将整个字符串的属性重置为新字符的属性(我没有设置,因为我不知道它们需要什么).

如何让tempAttr获取displayContent中最后一个字符的属性?谢谢.


更新.以笨拙但功能性的方式取得了进展.从显示中的最后一个字符(displayContent)复制属性字典,然后将这些属性重新应用于要添加的新字符:

NSMutableDictionary * lastCharAttrs = [NSMutableDictionary dictionaryWithCapacity:5];
[lastCharAttrs addEntriesFromDictionary: [displayContent attributesAtIndex:0 
                                                            effectiveRange:NULL]]; // get style of last letter
NSMutableAttributedString * tempAttr = [[NSMutableAttributedString alloc] initWithString:newCharacter 
                                                                              attributes:lastCharAttrs];

[displayContent appendAttributedString:tempAttr]; // Append to content in the display field
Run Code Online (Sandbox Code Playgroud)

我希望有一个更优雅的方法来设置NSTextField的属性.

objective-c nsattributedstring

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

如何设置HighlightedTextColor @ NSAttributedString

我有一个使用NSAttributedString的自定义UITableViewCell.我希望它在选择单元格时改变颜色.如何使NSAttributedString与具有highlightTextColor设置的UILabel具有相同的行为?

我试图改变单元格的函数setSelected和setHighlighted的颜色,但似乎它们被调用到很晚(在touchUpInside上而不是touchDown)

提前致谢!

iphone objective-c nsattributedstring

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

如何在UITableViewCell中的UITextView中一致地绘制NSAttributedString

我无法使用NSAttributedStrings在UITableViewCell中从UITextViews获得一致的结果.

内部 - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    headerText = [[UITextView alloc]initWithFrame:CGRectZero];
    [headerText setUserInteractionEnabled:NO];
    headerText.tag = HEADER_TEXT;
    [cell.contentView addSubview:headerText]; 
} else {
    headerText = (UITextView*)[cell.contentView viewWithTag:HEADER_TEXT];
}

//...setting up attributed strings


[headerText setAttributedText:headerString];

CGSize headerSize = [headerText sizeThatFits:CGSizeMake(246, CGFLOAT_MAX)];

headerText.frame = CGRectMake(45, 8, headerSize.width, headerSize.height);
Run Code Online (Sandbox Code Playgroud)

结果:

滚动之前没有内容设置

正如您所看到的,前两个似乎以我期望/想要的方式绘制文本.在最后两个中,UITextView sizeThatFits方法返回一个更大的大小,然后需要绘制文本,文本在框架中居中而不是紧紧到框架的顶部.这是一个问题,因为我希望能够根据uitextview框架高度布局其他视图.

滚出框架后返回: 在此输入图像描述

现在它变得更加奇怪,当重复使用单元格时,再次设置属性字符串.uitextview以不一致的方式绘制文本.

甚至将contentInsets设置为

headerText.contentInset = UIEdgeInsetsMake(-8, -8, -8, -8);
Run Code Online (Sandbox Code Playgroud)

不提供任何一致的结果:

在此输入图像描述

在使用contentinset设置滚动后: 在此输入图像描述

UITextView上是否有其他属性可以让我获得我需要的行为?

objective-c uitableview uitextview uikit nsattributedstring

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

如何用一些通用方法改变uilabel字符串部分的颜色?

任何人都可以告诉我如何用一些通用方法改变uilabel字符串部分的颜色?

让我解释 ..

我有标签

  "10/05/2008 4 years",
  "10/05/2011 2 years",
  "20/06/1933 - 07/07/1970 37 years",
  "10/05/1982 30 years",
  "10/05/2008 4 years",
  "10/05/2011 2 years",
  "20/06/1933 - 07/07/1970 37 years"
Run Code Online (Sandbox Code Playgroud)

现在我想要的是制作年份和它的价值,例如(4年)不同的颜色和日期用不同的字体的不同颜色.

NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:peopleDate.text];
    [string addAttribute:NSForegroundColorAttributeName value:GraycolorForText range:NSMakeRange(10, 5)];
    [peopleDate setAttributedText:string];
Run Code Online (Sandbox Code Playgroud)

这只会改变特定范围内的颜色而且不是通用的.所以任何人都可以给我任何解决方案吗?

iphone objective-c nsattributedstring uilabel ios

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

自定义NSAttributedString(iOS7 +)中的下划线模式

我正在尝试向NSAttributedString(在iOS7 + / TextKit上)添加点划线样式。当然,我尝试了内置功能NSUnderlinePatternDot:

NSString *string = self.label.text;
NSRange underlineRange = [string rangeOfString:@"consetetur sadipscing elitr"];

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
[attString addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot)} range:underlineRange];

self.label.attributedText = attString;
Run Code Online (Sandbox Code Playgroud)

但是,由此产生的样式实际上是虚线而不是虚线:

屏幕截图

我是否在这里缺少明显的东西(NSUnderlinePatternReallyDotted?;)?或者也许可以自定义线点图案?

underline nsattributedstring ios textkit

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

UITextView中的动画Gif

我看了一下这个问题并没有用.我也试图避免这种解决方案.最后,我愿意使用CADisplayLink并手动更改每个帧.不过,还有更简单的方法吗?

objective-c uitextview nsattributedstring ios swift

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

带有emojis的NSAttributedString结束时未格式化

我NSAttributedString在内容中使用表情符号的结尾没有被格式化.我正在尝试格式化整个字符串(在此示例中将文本颜色设置为白色,以简化),但是当放入文本时,某些文本保持未格式化UILabel.

在此输入图像描述

目前,我正在使用

let attributedString = NSMutableAttributedString(string: contents)

attributedString.addAttribute(
    NSForegroundColorAttributeName,
    value: UIColor.white,
    range: NSMakeRange(0, contents.characters.count)
)

label.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)

我也试过通过使用contents.utf8.count获得长度,但获得相同的结果.

我注意到未格式化的字符数与字符串中的表情符号数相同.这可能与正在发生的事情有关吗?

nsattributedstring uilabel ios nsmutableattributedstring swift

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

在Cocoa中使用辅助功能API插入格式化文本?

我正在尝试使用辅助功能API在Cocoa中插入一些格式化文本.这就是我所做的:

NSFont *font = [NSFont fontWithName:@"Arial" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"hello, world" attributes:attrsDictionary];
AXError error = AXUIElementSetAttributeValue(element, kAXValueDescriptionAttribute, attrString);
// element is an instance of 'AXUIElementRef' with role 'AXTextArea'
Run Code Online (Sandbox Code Playgroud)

我总是得到的错误是kAXErrorIllegalArgument (-25201).如果我尝试插入非格式化的文本,那么它工作正常.

那么,有没有人知道如何使用辅助功能API插入格式化文本?

cocoa accessibility objective-c nsattributedstring

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

为什么对NSMutableAttributedString addAttributes的调用会崩溃?

我在addAttributes无法重现的栏位(EXC_BREAKPOINT)上经常出车祸。在我传递的NSRange上添加了检查,令人惊讶的是它仍然崩溃...非常感谢收到任何想法!

let boldAttributes : [NSAttributedStringKey: Any] = [.font: cellStyle.boldFont()]
if (nsrange.location >= 0 && nsrange.length > 0 && nsrange.location + nsrange.length <= myAttributedString.length) {
    myAttributedString.addAttributes(boldAttributes, range: nsrange)
}
Run Code Online (Sandbox Code Playgroud)

通过请求添加的崩溃日志:

#0. Crashed: com.apple.main-thread
0  MyApp                   0x10054ae38 specialized UIAccessorizedTextField.accessoryAttributedString(IndexPath, cellStyle : UIAccessorizedTextFieldCellStyle) -> NSAttributedString (UIAccessorizedTextField.swift:322)
1  MyApp                   0x10054b104 specialized UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift:345)
2  MyApp                   0x10054860c @objc UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift)
3  UIKit                          0x18b229f3c -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 356
4  UIKit                          0x18bb90b68 -[UICollectionView _prefetchItemsForVelocity:maxItemsToPrefetch:invalidateCandidatesOnDirectionChanges:] + …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring core-text ios swift

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

包含exclusionPaths和视频的textView

我将照片放在textView中,如果文本中有视频,则将视频的照片替换为播放器,播放器出现了问题,我不明白为什么播放器位置不正确,我注意到replaceCharacters无法正常工作, copy? .replaceCharacters (in: videoRange [i - wrong], with: "this is correct position")仅替换第一张照片,copy? .replaceCharacters (in: videoRange [i - wrong], with: "+") 但全部替换错误的位置,例如replaceCharacters下面的工作方式

HTMLString是

<p>
     ???? ??-2 – ????????? ??? ????? (????) ?????????? ???????.
</p>
<p>
<iframe> <a href='https://www.youtube.com/watch?v=nXqyXRgkI_0'><img src="https://img.youtube.com/vi/nXqyXRgkI_0/0.jpg" alt="" width="600" /></a></iframe>
</p>
<p>
     ??????? ????????? ?????? ? ??????. ???? ????? ?????????? ??????????? ? ???????????? ?????. ??????? ?????? ???????? ???????? ????? ?? ??, ??? ?? ????? ??????? ???? ?? ??????. ????? ? ??? ???? …
Run Code Online (Sandbox Code Playgroud)

attachment nsattributedstring ios swift exclusionpath

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