diff --git a/src/Foundation/NSAttributedString.cs b/src/Foundation/NSAttributedString.cs index 6a2fdfb275d8..bb0657658531 100644 --- a/src/Foundation/NSAttributedString.cs +++ b/src/Foundation/NSAttributedString.cs @@ -25,6 +25,8 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +#nullable enable + using System; using CoreFoundation; using CoreText; @@ -36,13 +38,13 @@ namespace Foundation { public partial class NSAttributedString { - public string Value { + public string? Value { get { return CFString.FromHandle (LowLevelValue); } } - public NSDictionary GetAttributes (nint location, out NSRange effectiveRange) + public NSDictionary? GetAttributes (nint location, out NSRange effectiveRange) { return Runtime.GetNSObject (LowLevelGetAttributes (location, out effectiveRange)); } @@ -58,21 +60,21 @@ public IntPtr LowLevelGetAttributes (nint location, out NSRange effectiveRange) } #endif - public NSAttributedString (string str, CTStringAttributes attributes) - : this (str, attributes != null ? attributes.Dictionary : null) + public NSAttributedString (string str, CTStringAttributes? attributes) + : this (str, attributes?.Dictionary) { } - public CTStringAttributes GetCoreTextAttributes (nint location, out NSRange effectiveRange) + public CTStringAttributes? GetCoreTextAttributes (nint location, out NSRange effectiveRange) { var attr = GetAttributes (location, out effectiveRange); - return attr == null ? null : new CTStringAttributes (attr); + return attr is null ? null : new CTStringAttributes (attr); } - public CTStringAttributes GetCoreTextAttributes (nint location, out NSRange longestEffectiveRange, NSRange rangeLimit) + public CTStringAttributes? GetCoreTextAttributes (nint location, out NSRange longestEffectiveRange, NSRange rangeLimit) { var attr = GetAttributes (location, out longestEffectiveRange, rangeLimit); - return attr == null ? null : new CTStringAttributes (attr); + return attr is null ? null : new CTStringAttributes (attr); } public NSAttributedString Substring (nint start, nint len) @@ -81,52 +83,52 @@ public NSAttributedString Substring (nint start, nint len) } #if !MONOMAC - public NSAttributedString (string str, UIStringAttributes attributes) - : this (str, attributes != null ? attributes.Dictionary : null) + public NSAttributedString (string str, UIStringAttributes? attributes) + : this (str, attributes?.Dictionary) { } - public UIStringAttributes GetUIKitAttributes (nint location, out NSRange effectiveRange) + public UIStringAttributes? GetUIKitAttributes (nint location, out NSRange effectiveRange) { var attr = GetAttributes (location, out effectiveRange); - return attr == null ? null : new UIStringAttributes (attr); + return attr is null ? null : new UIStringAttributes (attr); } - public UIStringAttributes GetUIKitAttributes (nint location, out NSRange longestEffectiveRange, NSRange rangeLimit) + public UIStringAttributes? GetUIKitAttributes (nint location, out NSRange longestEffectiveRange, NSRange rangeLimit) { var attr = GetAttributes (location, out longestEffectiveRange, rangeLimit); - return attr == null ? null : new UIStringAttributes (attr); + return attr is null ? null : new UIStringAttributes (attr); } - static internal NSDictionary ToDictionary ( - UIFont font, - UIColor foregroundColor, - UIColor backgroundColor, - UIColor strokeColor, - NSParagraphStyle paragraphStyle, + static internal NSDictionary? ToDictionary ( + UIFont? font, + UIColor? foregroundColor, + UIColor? backgroundColor, + UIColor? strokeColor, + NSParagraphStyle? paragraphStyle, NSLigatureType ligature, float kerning, NSUnderlineStyle underlineStyle, #if !WATCH - NSShadow shadow, + NSShadow? shadow, #endif float strokeWidth, NSUnderlineStyle strikethroughStyle) { var attr = new UIStringAttributes (); - if (font != null) { + if (font is not null) { attr.Font = font; } - if (foregroundColor != null) { + if (foregroundColor is not null) { attr.ForegroundColor = foregroundColor; } - if (backgroundColor != null) { + if (backgroundColor is not null) { attr.BackgroundColor = backgroundColor; } - if (strokeColor != null) { + if (strokeColor is not null) { attr.StrokeColor = strokeColor; } - if (paragraphStyle != null) { + if (paragraphStyle is not null) { attr.ParagraphStyle = paragraphStyle; } if (ligature != NSLigatureType.Default) { @@ -139,7 +141,7 @@ static internal NSDictionary ToDictionary ( attr.UnderlineStyle = underlineStyle; } #if !WATCH - if (shadow != null) { + if (shadow is not null) { attr.Shadow = shadow; } #endif @@ -154,16 +156,16 @@ static internal NSDictionary ToDictionary ( } public NSAttributedString (string str, - UIFont font = null, - UIColor foregroundColor = null, - UIColor backgroundColor = null, - UIColor strokeColor = null, - NSParagraphStyle paragraphStyle = null, + UIFont? font = null, + UIColor? foregroundColor = null, + UIColor? backgroundColor = null, + UIColor? strokeColor = null, + NSParagraphStyle? paragraphStyle = null, NSLigatureType ligatures = NSLigatureType.Default, float kerning = 0, NSUnderlineStyle underlineStyle = NSUnderlineStyle.None, #if !WATCH - NSShadow shadow = null, + NSShadow? shadow = null, #endif float strokeWidth = 0, NSUnderlineStyle strikethroughStyle = NSUnderlineStyle.None) @@ -183,7 +185,7 @@ public NSAttributedString (string str, // note: we cannot reuse the same method name - as it would break compilation of existing apps public static NSAttributedString CreateFrom (NSTextAttachment attachment) { - return (null as NSAttributedString).FromTextAttachment (attachment); + return (null as NSAttributedString)!.FromTextAttachment (attachment); } #endif #endif diff --git a/src/Foundation/NSAttributedString.iOS.cs b/src/Foundation/NSAttributedString.iOS.cs index 10359c6dcbc4..73811f4c9199 100644 --- a/src/Foundation/NSAttributedString.iOS.cs +++ b/src/Foundation/NSAttributedString.iOS.cs @@ -25,6 +25,8 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +#nullable enable + using System; using ObjCRuntime; @@ -39,7 +41,7 @@ namespace Foundation { #if !MONOMAC && !COREBUILD public partial class NSAttributedString { - static NSDictionary ignore; + static NSDictionary? ignore; public NSAttributedString (NSUrl url, NSAttributedStringDocumentAttributes documentAttributes, ref NSError error) : this (url, documentAttributes, out ignore, ref error) { } @@ -48,14 +50,14 @@ public NSAttributedString (NSData data, NSAttributedStringDocumentAttributes doc : this (data, documentAttributes, out ignore, ref error) { } public NSAttributedString (NSUrl url, ref NSError error) - : this (url, (NSDictionary) null, out ignore, ref error) { } + : this (url, new NSDictionary (), out ignore, ref error) { } public NSAttributedString (NSData data, ref NSError error) - : this (data, (NSDictionary) null, out ignore, ref error) { } + : this (data, new NSDictionary (), out ignore, ref error) { } #if IOS // not TVOS or WATCH // use the best selector based on the OS version - public NSAttributedString (NSUrl url, NSDictionary options, out NSDictionary resultDocumentAttributes, ref NSError error) + public NSAttributedString (NSUrl url, NSDictionary? options, out NSDictionary resultDocumentAttributes, ref NSError error) { if (SystemVersion.CheckiOS (9,0)) Handle = InitWithURL (url, options, out resultDocumentAttributes, ref error); @@ -73,12 +75,12 @@ public NSAttributedString (NSUrl url, NSDictionary options, out NSDictionary res public partial class NSAttributedStringDocumentAttributes : DictionaryContainer { #if !MONOMAC && !COREBUILD public NSAttributedStringDocumentAttributes () : base (new NSMutableDictionary ()) { } - public NSAttributedStringDocumentAttributes (NSDictionary dictionary) : base (dictionary) { } + public NSAttributedStringDocumentAttributes (NSDictionary? dictionary) : base (dictionary) { } public NSStringEncoding? StringEncoding { get { var value = GetInt32Value (UIStringAttributeKey.NSCharacterEncodingDocumentAttribute); - if (value == null) + if (value is null) return null; else return (NSStringEncoding) value.Value; @@ -88,7 +90,7 @@ public NSStringEncoding? StringEncoding { } } - public NSString WeakDocumentType { + public NSString? WeakDocumentType { get { return GetNSStringValue (UIStringAttributeKey.NSDocumentTypeDocumentAttribute); } @@ -139,7 +141,7 @@ public CGSize? PaperSize { return null; } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSPaperSizeDocumentAttribute); else Dictionary [UIStringAttributeKey.NSPaperSizeDocumentAttribute] = NSValue.FromCGSize (value.Value); @@ -156,7 +158,7 @@ public UIEdgeInsets? PaperMargin { return null; } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSPaperMarginDocumentAttribute); else Dictionary [UIStringAttributeKey.NSPaperMarginDocumentAttribute] = NSValue.FromUIEdgeInsets (value.Value); @@ -173,7 +175,7 @@ public CGSize? ViewSize { return null; } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSViewSizeDocumentAttribute); else Dictionary [UIStringAttributeKey.NSViewSizeDocumentAttribute] = NSValue.FromCGSize (value.Value); @@ -185,7 +187,7 @@ public float? ViewZoom { return GetFloatValue (UIStringAttributeKey.NSViewZoomDocumentAttribute); } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSViewZoomDocumentAttribute); else SetNumberValue (UIStringAttributeKey.NSViewZoomDocumentAttribute, value); @@ -195,13 +197,13 @@ public float? ViewZoom { public NSDocumentViewMode? ViewMode { get { var value = GetInt32Value (UIStringAttributeKey.NSViewModeDocumentAttribute); - if (value == null) + if (value is null) return null; else return (NSDocumentViewMode) value.Value; } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSViewModeDocumentAttribute); else SetNumberValue (UIStringAttributeKey.NSViewModeDocumentAttribute, (int) value.Value); @@ -211,7 +213,7 @@ public NSDocumentViewMode? ViewMode { public bool ReadOnly { get { var value = GetInt32Value (UIStringAttributeKey.NSReadOnlyDocumentAttribute); - if (value == null || value.Value <= 0) + if (value is null || value.Value <= 0) return false; return true; } @@ -220,14 +222,14 @@ public bool ReadOnly { } } - public UIColor BackgroundColor { + public UIColor? BackgroundColor { get { - NSObject value; + NSObject? value; Dictionary.TryGetValue (UIStringAttributeKey.NSBackgroundColorDocumentAttribute, out value); return value as UIColor; } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSBackgroundColorDocumentAttribute); else Dictionary [UIStringAttributeKey.NSBackgroundColorDocumentAttribute] = value; @@ -239,7 +241,7 @@ public float? HyphenationFactor { return GetFloatValue (UIStringAttributeKey.NSReadOnlyDocumentAttribute); } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSReadOnlyDocumentAttribute); else { if (value < 0 || value > 1.0f) @@ -254,7 +256,7 @@ public float? DefaultTabInterval { return GetFloatValue (UIStringAttributeKey.NSDefaultTabIntervalDocumentAttribute); } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSDefaultTabIntervalDocumentAttribute); else { if (value < 0 || value > 1.0f) @@ -264,14 +266,14 @@ public float? DefaultTabInterval { } } - public NSDictionary WeakDefaultAttributes { + public NSDictionary? WeakDefaultAttributes { get { - NSObject value; + NSObject? value; Dictionary.TryGetValue (UIStringAttributeKey.NSDefaultAttributesDocumentAttribute, out value); return value as NSDictionary; } set { - if (value == null) + if (value is null) RemoveValue (UIStringAttributeKey.NSDefaultAttributesDocumentAttribute); else Dictionary [UIStringAttributeKey.NSDefaultAttributesDocumentAttribute] = value; @@ -290,13 +292,13 @@ public NSDictionary WeakDefaultAttributes { [Mac (10, 15)] [iOS (13, 0)] #endif - public NSUrl ReadAccessUrl { + public NSUrl? ReadAccessUrl { get { Dictionary.TryGetValue (NSAttributedStringDocumentReadingOptionKeys.ReadAccessUrlKey, out var value); return value as NSUrl; } set { - if (value == null) + if (value is null) RemoveValue (NSAttributedStringDocumentReadingOptionKeys.ReadAccessUrlKey); else Dictionary [NSAttributedStringDocumentReadingOptionKeys.ReadAccessUrlKey] = value; diff --git a/src/Foundation/NSAttributedString.mac.cs b/src/Foundation/NSAttributedString.mac.cs index 2edc7e0ff903..ae1a42d44487 100644 --- a/src/Foundation/NSAttributedString.mac.cs +++ b/src/Foundation/NSAttributedString.mac.cs @@ -6,6 +6,8 @@ // // Copyright 2013 Xamarin Inc +#nullable enable + #if MONOMAC using System; @@ -18,39 +20,39 @@ namespace Foundation { public partial class NSAttributedString { - public NSAttributedString (string str, NSStringAttributes attributes) - : this (str, attributes != null ? attributes.Dictionary : null) + public NSAttributedString (string str, NSStringAttributes? attributes) + : this (str, attributes?.Dictionary) { } public NSAttributedString (string str, - NSFont font = null, - NSColor foregroundColor = null, - NSColor backgroundColor = null, - NSColor strokeColor = null, - NSColor underlineColor = null, - NSColor strikethroughColor = null, + NSFont? font = null, + NSColor? foregroundColor = null, + NSColor? backgroundColor = null, + NSColor? strokeColor = null, + NSColor? underlineColor = null, + NSColor? strikethroughColor = null, NSUnderlineStyle underlineStyle = NSUnderlineStyle.None, NSUnderlineStyle strikethroughStyle = NSUnderlineStyle.None, - NSParagraphStyle paragraphStyle = null, + NSParagraphStyle? paragraphStyle = null, float strokeWidth = 0, - NSShadow shadow = null, - NSUrl link = null, + NSShadow? shadow = null, + NSUrl? link = null, bool superscript = false, - NSTextAttachment attachment = null, + NSTextAttachment? attachment = null, NSLigatureType ligature = NSLigatureType.Default, float baselineOffset = 0, float kerningAdjustment = 0, float obliqueness = 0, float expansion = 0, - NSCursor cursor = null, - string toolTip = null, + NSCursor? cursor = null, + string? toolTip = null, int characterShape = 0, - NSGlyphInfo glyphInfo = null, - NSArray writingDirection = null, + NSGlyphInfo? glyphInfo = null, + NSArray? writingDirection = null, bool markedClauseSegment = false, NSTextLayoutOrientation verticalGlyphForm = NSTextLayoutOrientation.Horizontal, - NSTextAlternatives textAlternatives = null, + NSTextAlternatives? textAlternatives = null, NSSpellingState spellingState = NSSpellingState.None) : this (str, NSStringAttributes.ToDictionary ( font: font, foregroundColor: foregroundColor, @@ -127,16 +129,16 @@ public static NSAttributedString CreateWithDocFormat (NSData wordDocFormat, out return new NSAttributedString (wordDocFormat, NSAttributedStringDataType.DocFormat, out docAttributes); } - public NSStringAttributes GetAppKitAttributes (nint location, out NSRange effectiveRange) + public NSStringAttributes? GetAppKitAttributes (nint location, out NSRange effectiveRange) { var attr = GetAttributes (location, out effectiveRange); - return attr == null ? null : new NSStringAttributes (attr); + return attr is null ? null : new NSStringAttributes (attr); } - public NSStringAttributes GetAppKitAttributes (nint location, out NSRange longestEffectiveRange, NSRange rangeLimit) + public NSStringAttributes? GetAppKitAttributes (nint location, out NSRange longestEffectiveRange, NSRange rangeLimit) { var attr = GetAttributes (location, out longestEffectiveRange, rangeLimit); - return attr == null ? null : new NSStringAttributes (attr); + return attr is null ? null : new NSStringAttributes (attr); } } @@ -144,27 +146,27 @@ public partial class NSAttributedStringDocumentAttributes : DictionaryContainer public NSAttributedStringDocumentAttributes () : base (new NSMutableDictionary ()) {} public NSAttributedStringDocumentAttributes (NSDictionary dictionary) : base (dictionary) {} - public WebPreferences WebPreferences { + public WebPreferences? WebPreferences { get { NSObject value; Dictionary.TryGetValue (NSStringAttributeKey.NSWebPreferencesDocumentOption, out value); return value as WebPreferences; } set { - if (value == null) + if (value is null) RemoveValue (NSStringAttributeKey.NSWebPreferencesDocumentOption); else Dictionary [NSStringAttributeKey.NSWebPreferencesDocumentOption] = value; } } - public NSObject WebResourceLoadDelegate { + public NSObject? WebResourceLoadDelegate { get { NSObject value; Dictionary.TryGetValue (NSStringAttributeKey.NSWebResourceLoadDelegateDocumentOption, out value); return value; } set { - if (value == null) + if (value is null) RemoveValue (NSStringAttributeKey.NSWebResourceLoadDelegateDocumentOption); else Dictionary [NSStringAttributeKey.NSWebResourceLoadDelegateDocumentOption] = value; @@ -174,7 +176,7 @@ public NSObject WebResourceLoadDelegate { public NSStringEncoding? StringEncoding { get { var value = GetInt32Value (NSStringAttributeKey.NSCharacterEncodingDocumentOption); - if (value == null) + if (value is null) return null; else return (NSStringEncoding) value.Value; @@ -184,7 +186,7 @@ public NSStringEncoding? StringEncoding { } } - public NSString WeakDocumentType { + public NSString? WeakDocumentType { get { return GetNSStringValue (NSStringAttributeKey.NSDocumentTypeDocumentOption); } @@ -256,21 +258,21 @@ public NSDocumentType DocumentType { } } - public NSDictionary WeakDefaultAttributes { + public NSDictionary? WeakDefaultAttributes { get { NSObject value; Dictionary.TryGetValue (NSStringAttributeKey.NSDefaultAttributesDocumentOption, out value); return value as NSDictionary; } set { - if (value == null) + if (value is null) RemoveValue (NSStringAttributeKey.NSDefaultAttributesDocumentOption); else Dictionary [NSStringAttributeKey.NSDefaultAttributesDocumentOption] = value; } } - public NSUrl BaseUrl { + public NSUrl? BaseUrl { get { return GetNativeValue (NSStringAttributeKey.NSBaseURLDocumentOption); } @@ -279,7 +281,7 @@ public NSUrl BaseUrl { } } - public string TextEncodingName { + public string? TextEncodingName { get { return (string)GetNSStringValue (NSStringAttributeKey.NSTextEncodingNameDocumentOption); }