我有一个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的属性.
我有一个使用NSAttributedString的自定义UITableViewCell.我希望它在选择单元格时改变颜色.如何使NSAttributedString与具有highlightTextColor设置的UILabel具有相同的行为?
我试图改变单元格的函数setSelected和setHighlighted的颜色,但似乎它们被调用到很晚(在touchUpInside上而不是touchDown)
提前致谢!
我无法使用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上是否有其他属性可以让我获得我需要的行为?
任何人都可以告诉我如何用一些通用方法改变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)
这只会改变特定范围内的颜色而且不是通用的.所以任何人都可以给我任何解决方案吗?
我正在尝试向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?;)?或者也许可以自定义线点图案?
我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
我正在尝试使用辅助功能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插入格式化文本?
我在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) 我将照片放在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) ios ×6
objective-c ×6
swift ×4
iphone ×2
uilabel ×2
uitextview ×2
attachment ×1
cocoa ×1
core-text ×1
textkit ×1
uikit ×1
uitableview ×1
underline ×1