Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions src/Foundation/NSAttributedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#nullable enable

using System;
using CoreFoundation;
using CoreText;
Expand All @@ -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<NSDictionary> (LowLevelGetAttributes (location, out effectiveRange));
}
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
50 changes: 26 additions & 24 deletions src/Foundation/NSAttributedString.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#nullable enable

using System;

using ObjCRuntime;
Expand All @@ -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) { }
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -88,7 +90,7 @@ public NSStringEncoding? StringEncoding {
}
}

public NSString WeakDocumentType {
public NSString? WeakDocumentType {
get {
return GetNSStringValue (UIStringAttributeKey.NSDocumentTypeDocumentAttribute);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading