标签: nsattributedstring

NSAttributedStringboundingRect 返回错误的高度

NSAttributedString我这样计算一个高度。

let maxSize = NSSize(width: w, height: CGFloat.greatestFiniteMagnitude)
let rect = boundingRect(with: maxSize, options: [.usesFontLeading, .usesLineFragmentOrigin])
let height = rect.integral.size.height
Run Code Online (Sandbox Code Playgroud)

我尝试了SO中提到的每一个“hack”,但是宽度越小,字符串高度就越不准确(计算出的高度大于实际高度)。

根据其他帖子,以下原因会导致尺寸计算出现问题:

  • 空白
  • 无前景色
  • 无背景颜色
  • 宽度低于 300 不起作用(?)

我发现这些建议都没有任何作用。属性字符串是行的串联,每行都有 aforegroundColorbackgroundColor。该字符串还具有以下段落样式。

 let pstyle = NSMutableParagraphStyle()
 pstyle.lineSpacing = 6
 pstyle.lineBreakMode = .byWordWrapping
Run Code Online (Sandbox Code Playgroud)

userFixedPitchFont11 号的。

为什么宽度越小高度误差越大?

PS:我想这与此有关,lineBreak因为如果更多行被自动换行,错误会变得更大。

macos cocoa nsattributedstring swift

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

iOS:向 NSAttributedString 添加 underlineStyle 属性,使文本视图消失

我在故事板中创建了一个 UITextView。

向字符串添加属性。

一切顺利,直到我添加 underlineStyle 属性。

然后文本视图消失。

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let text = "random text <a href='http://www.google.com'>http://www.google.com </a> more random text"

        let attributedText = htmlStyleAttributeText(text: text)!

        let underLineColor: UIColor = UIColor(red: 245/255, green: 190/255, blue: 166/255, alpha: 1)

        let attributes: [NSAttributedString.Key: Any] = 
            [.underlineColor: underLineColor,
             .font: UIFont.systemFont(ofSize: 25),
             .underlineStyle: NSUnderlineStyle.thick] // Adding this makes the UITextView disappear. 

        let wholeRange = NSRange(attributedText.string.startIndex..., in: attributedText.string)
        attributedText.addAttributes(attributes, range: wholeRange)
        textView.attributedText = attributedText …
Run Code Online (Sandbox Code Playgroud)

uitextfield nsattributedstring ios swift

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

目标C - 完成后表格标签上的删除线

这里有一些工作代码,它在完成时为待办事项添加一个复选标记:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
         cell.accessoryType = UITableViewCellAccessoryNone;
    }    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是删除复选标记代码和类似的东西(它的基本逻辑):

    if (toDoItem.completed) {
        cellIdentifier.textLabel (NSAttributedString Strikethrough = YES)

    } else {
         cellIdentifier.textLabel (NSAttributedString Strikethrough = NO)
    }    return cell;
Run Code Online (Sandbox Code Playgroud)

我也试图改变NSString,以NSAttributedStringNSMutableAttributedString基于我已经看到了一些其他的问题和答案,如这一个是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary* …
Run Code Online (Sandbox Code Playgroud)

objective-c nsstring nsattributedstring strikethrough ios

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

在iOS中,带有属性文本的textview的高度有时是错误的

我用以下代码强制我的textview的高度约束

func setHeightConstraint(textView: UITextView) {
    textView.sizeToFit()
    textView.layoutIfNeeded()

    var newFrame:CGRect=textView.frame
    newFrame.size.height=textView.contentSize.height
    textView.frame=newFrame
    println(textView.contentSize.height)
    textViewHeightConstraints.constant=textView.contentSize.height
}
Run Code Online (Sandbox Code Playgroud)

我的文本视图包含带有链接的boldText,粗体,斜体等等.

它有时效果很好,但在其他时候却没有.

我通过打印textView.contentSize.height来计算,textView.contentSize.height有时比它应该小得多.

我使用普通文本的代码片段没有问题,所以我想这是关于attributionText的问题.

我尝试使用谷歌搜索并尝试了这个代码没有运气.

当它包含attributesText时,我应该如何测量textView的正确高度?

任何帮助将不胜感激.

提前致谢!

(我也可以阅读Object-C代码,虽然我更喜欢swift,所以对象-C答案也很受欢迎!!)

uitextview nsattributedstring ios

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

在Swift中使用普通和粗体文本标签的iOS选择器视图

我需要一个带有一些条目的选择器视图,它具有正常的字体粗细,一些具有粗体字体重.找出如何创建一个属性字符串不是问题,所以我把所有东西放在一起,但我看不出有什么区别.第一行不是粗体而不是更大.不过,不同的颜色确实有用.

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let darkColor = UIColor.blackColor()
    let lightColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3)                             
    if row == 0 {
        return NSAttributedString(string: "first row bold and black", attributes: [NSForegroundColorAttributeName:darkColor, NSFontAttributeName : UIFont.boldSystemFontOfSize(20)])
    } else {
        return NSAttributedString(string: "other rows gray and normal", attributes: [NSForegroundColorAttributeName:lightColor])
    }
}
Run Code Online (Sandbox Code Playgroud)

uipickerview nsattributedstring ios swift

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

以特定方式将String转换为NSAttributedString

所以我有一个看起来像这样的字符串: Swift , VisualBasic , Ruby

我想把这个字符串转换成这样的东西: 在此输入图像描述

基本上我想创建一个单词后面的背景,是的我可以使用NSTokenField库来获取此行为,但我的文本不是由用户手动输入其预结构(从数组),我不想要NSTokeField的整个行为我只是想要这样的外观和选择(通过选择我的意思是在退格时一次点击清除一个单词,整个单词不是一个字母)

我知道如何改变这样的文字颜色

   func getColoredText(text: String) -> NSMutableAttributedString {
    let string:NSMutableAttributedString = NSMutableAttributedString(string: text)
    let words:[String] = text.componentsSeparatedByString(" ")
    var w = ""

    for word in words {
        if (word.hasPrefix("{|") && word.hasSuffix("|}")) {
            let range:NSRange = (string.string as NSString).rangeOfString(word)
            string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range)
            w = word.stringByReplacingOccurrencesOfString("{|", withString: "")
            w = w.stringByReplacingOccurrencesOfString("|}", withString: "")
            string.replaceCharactersInRange(range, withString: w)
        }
    }
    return string
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何实现我想要的,如果有人可以提供一些指导,那么它对我来说会很有帮助

如果我的问题不够清楚,请告诉我,我会添加更多细节

nsattributedstring ios swift2

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

如何在UILabel中添加函数调用(不是超链接)到NSAttributedString的一部分?

我需要在文本的某些部分添加一些tapGestures,即UILabel中的那些部分.它似乎能够创建一个超链接 - 所以我想它也可以做一个函数调用,但我只是不知道该怎么做.

继承我的代码:

    let regularFont = UIFont(name: Constants.robotoRegular, size: 13)!
    let att1 = [NSFontAttributeName: regularFont]

    let turqoiseFont = UIFont(name: Constants.robotoBold, size: 13)!
    let att2 = [NSFontAttributeName: turqoiseFont]

    let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: att1)
    attString1.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString1.length))

    let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: att2)
    attString2.addAttribute(NSForegroundColorAttributeName, value: Colors.loginButtonColor, range: NSMakeRange(0, attString2.length))

    let attString3 = NSMutableAttributedString(string: "and ", attributes: att1)
    attString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString3.length))

    let attString4 = …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel ios uitapgesturerecognizer swift

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

在Swift中只有下划线的颜色

如何更改标签中下划线的颜色?我只希望下划线更改颜色,而不希望更改整个文本。

我已使用此代码来强调以下内容:

let underlineAttribute = [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue]
let underlineAttributedString = NSAttributedString(string: "\(nearSavings[indexPath.row]) ,-", attributes: underlineAttribute)
cell.detailTextLabel?.attributedText = underlineAttributedString
Run Code Online (Sandbox Code Playgroud)

但是我找不到代码来设置下划线颜色。谁能帮忙?

label underline nsattributedstring swift

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

如何获得属性字符串的字符串范围?

我有字符串作为2/42。这里2和42是动态的,现在我想设置/42为字符串的上标。我无法找到的范围/42。请告诉我如何迅速找到范围?

range nsattributedstring ios swift swift4

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

UITextView / NSAttribute:检测单词是否以特定符号开头

我一直在寻找一种方法来更改UITextView单词中以"@"或开头的文本"#"。我在下面的StackOverflow上找到了此代码段,如果您键入"Hello"或,则可以完美运行"World"

我如何调整此代码,以便它可以检测单词是否以“ @”或“#”开头,​​后跟空格之前的任意数量的字符,并应用相同的样式?

UITextView如果用户以"@"或开头“单词”,结果将导致文本颜色发生变化"#"。即:

快速的棕色#fox跳过了@lazydog

func textViewDidChange(_ textView: UITextView) {
    let defaultAttributes = mediaDescription.attributedText.attributes(at: 0, effectiveRange: nil)
    let attrStr = NSMutableAttributedString(string: textView.text, attributes: defaultAttributes)
    let inputLength = attrStr.string.count
    let searchString : NSArray = NSArray.init(objects: "hello", "world")
    for i in 0...searchString.count-1
    {
        let string : String = searchString.object(at: i) as! String
        let searchLength = string.count
        var range = NSRange(location: 0, …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios swift

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