-2

以下代码在我的应用程序中用于在粗体和斜体之间更改文本视图中文本的状态。我一直在尝试用 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];

}

SWIFT 代码: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)
    }

}

4

1 回答 1

5

根据's "Symbolic Font Traits"的文档,UIFontDescriptor在Objective-C 中UIFontDescriptorSymbolicTraits是用 type 定义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))
}

如果你仍然需要访问你可以请求原始值的值UInt32UIFontDescriptorSymbolicTraits

var existingTraitsWithNewTraitUInt32:UInt32 = existingTraitsWithNewTrait.rawValue
于 2015-03-10T05:08:58.367 回答