【问题标题】:Convert Object-C to SWIFT textKit将 Object-C 转换为 SWIFT textKit
【发布时间】:2015-03-09 01:37:33
【问题描述】:

在我的应用程序中使用以下代码在粗体和斜体之间更改文本视图中文本的状态。我一直在尝试用 Swift 重写它,但我不断收到编译错误。以下是在线教程中提供给我的 Objective-C 版本。接下来是我的新 swift 版本,直到该方法崩溃为止。
我会感谢任何可能帮助我理解为什么编译器抱怨这么多的帮助。

-(void)addOrRemoveFontTraitWithName:(NSString *)traitName andValue:(uint32_t)traitValue{
NSRange selectedRange = [_field2 selectedRange];


NSDictionary *currentAttributesDict = [_field2.textStorage attributesAtIndex:selectedRange.location
                                                                effectiveRange:nil];


UIFont *currentFont = [currentAttributesDict objectForKey:NSFontAttributeName];
UIColor *currentColor = [currentAttributesDict objectForKey:NSForegroundColorAttributeName];
UIColor *currentBGColor = [currentAttributesDict objectForKey:NSBackgroundColorAttributeName];
UIFont *currentUnderlinedText = [currentAttributesDict objectForKey:NSUnderlineStyleAttributeName];
NSMutableParagraphStyle *currentparagraphStyle = [currentAttributesDict objectForKey:NSParagraphStyleAttributeName];    //receive and set new font name



UIFontDescriptor *fontDescriptor = [currentFont fontDescriptor];


NSString *fontNameAttribute = [[fontDescriptor fontAttributes] objectForKey:UIFontDescriptorNameAttribute];
UIFontDescriptor *changedFontDescriptor;

if ([fontNameAttribute rangeOfString:traitName].location == NSNotFound) {


    uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | traitValue;
    changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];

}
else{
    uint32_t existingTraitsWithoutTrait = [fontDescriptor symbolicTraits] & ~traitValue;
    changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithoutTrait];

}
UIFont *updatedFont = [UIFont fontWithDescriptor:changedFontDescriptor size:0.0];

//create new NSDict. called dict.
NSMutableDictionary *dict;
//call below function that returns an NSMutableDict. and places value to dict.
dict = [self checkSlectedAttributes:updatedFont SelFontColor:currentColor SelFontBGColor:currentBGColor SelunderLineState:currentUnderlinedText paragraphStyle:currentparagraphStyle];

// NSLog(@"字典是:%@", dict);

[_field2.textStorage beginEditing];
[_field2.textStorage setAttributes:dict range:selectedRange];
[_field2.textStorage endEditing];

}

快速代码: func addOrRemoveFontTraitWithName(traitName: String!, andValue traitValue: UInt32) {

    let selectedRange : NSRange = self.textView.selectedRange

    var currentAttributesDict : NSDictionary = textView.textStorage.attributesAtIndex(selectedRange.location, effectiveRange: nil)

    var currentFont : UIFont = currentAttributesDict .objectForKey(NSFontAttributeName) as UIFont

    var currentColor : UIColor = currentAttributesDict.objectForKey(NSForegroundColorAttributeName) as UIColor
    var currentBGColor : UIColor = currentAttributesDict.objectForKey(NSBackgroundColorAttributeName) as UIColor
    var currentUnderLinedText : UIFont = currentAttributesDict.objectForKey(NSUnderlineStyleAttributeName) as UIFont
    var currentparagraphStyle : NSMutableParagraphStyle = currentAttributesDict.objectForKey(NSParagraphStyleAttributeName) as NSMutableParagraphStyle

    var fontDescriptor : UIFontDescriptor = currentFont.fontDescriptor()
    var fontNameAttribute : NSString = fontDescriptor.objectForKey(UIFontDescriptorNameAttribute) as NSString
    var changedFontDescriptor : UIFontDescriptor

    if (fontNameAttribute.rangeOfString(traitName).location == NSNotFound){
       var existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits | traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait)
    }else{

        var existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits & ~traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait)
    }

}

【问题讨论】:

  • 你具体在哪个部分苦苦挣扎?
  • if 语句中的所有内容。当我尝试使用 swift 编写这个 if 语句时,我收到了关于 | 的抱怨。并且 & simbles 并且 uint32_t 到 fontDescriptor 存在转换错误
  • 我建议发布您当前的 swift 代码,以便我们查看您做错了什么。
  • 我在尝试编写以下语句时遇到了问题: var existingTraitsWithNewTrait = fontDescriptor.symbolicTraits | traitValue ,我得到的错误是:无法调用'|'带有类型为“(UIFontDescriptorSymbolicTraits,UInt32)”的参数列表我希望这有助于我的解释。

标签: ios objective-c swift uifontdescriptor


【解决方案1】:

根据the docs for UIFontDescriptor's "Symbolic Font Traits"UIFontDescriptorSymbolicTraits 在 Objective-C 中定义为 uint32_t 类型,但在 Swift 中没有。

尝试将您的代码更改为以下内容:

if fontNameAttribute.rangeOfString(traitName).location == NSNotFound {
    let existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits.rawValue | traitValue
    changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))
} else {
    let existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits.rawValue & traitValue
    changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))
}

如果您仍需要访问UIFontDescriptorSymbolicTraitsUInt32 值,您可以请求原始值:

var existingTraitsWithNewTraitUInt32:UInt32 = existingTraitsWithNewTrait.rawValue

【讨论】:

  • 您的第一个示例几乎是我已经在尝试的,但 rawValue 看起来很有希望。当我使用 .rawValue 时,if 语句中的第一个代码块有效,但第二行抱怨 UInt32 不能转换为 UIFontDescriptorSymbolicTraits。
  • @EdwardG 第一段代码与您发布的代码不同。你真的试过了吗?因为它对我有用...您不需要在当前代码中将任何内容转换为 UInt32。
  • 是的,我遇到了同样的错误。作为一名业余程序员,我正在尝试将代码从 Objective-C 转换为 Swift,因为这似乎是最直接的方法,但在 SWIFT 中可能有一种不同或更简单的方法可以在粗体和斜体之间切换。我不明白的一件事是按位运算符的使用,我不明白它们的用途以及“|”(或)和“&”(和)符号的用途。
  • @EdwardG 我没有收到该代码的错误,因此您的代码必须在某些方面有所不同。
  • Swift 编译器错误:无法调用 '|'带有类型为 '(UIFontDescriptorSymbolicTraits, UInt32) 的参数列表
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
相关资源
最近更新 更多