Skip to content

Commit 5129306

Browse files
rolfbjarneGitHub Copilot
andauthored
[Foundation] Fix nullability in NSMutableAttributedString. (#24431)
- Enabled nullable reference types - Made attribute parameters nullable where appropriate - Updated null checks to use proper null handling patterns - Improved XML documentation: - Removed 'To be added.' placeholders - Added comprehensive descriptions for all public members - Added see cref attributes for related types - Fixed formatting and consistency - Fixed potential null reference in Append method's ToString() call This is file 35 of 47 files with nullability disabled in Foundation. Contributes towards #17285. --------- Co-authored-by: GitHub Copilot <copilot@github.com>
1 parent cf7f48f commit 5129306

File tree

2 files changed

+83
-73
lines changed

2 files changed

+83
-73
lines changed

src/Foundation/NSMutableAttributedString.cs

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,108 +33,113 @@
3333

3434
using CoreText;
3535

36-
// Disable until we get around to enable + fix any issues.
37-
#nullable disable
36+
#nullable enable
3837

3938
namespace Foundation {
4039

4140
public partial class NSMutableAttributedString {
4241

43-
/// <param name="str">C# string.</param>
44-
/// <param name="attributes">CoreText attributes to be applied to the string.</param>
45-
/// <summary>Creates an NSMutableAttributedString from a C# string and applies the specified CoreText attributes to the entire string.</summary>
46-
/// <remarks>
47-
/// </remarks>
48-
public NSMutableAttributedString (string str, CTStringAttributes attributes)
49-
: this (str, attributes is null ? null : attributes.Dictionary)
42+
/// <summary>
43+
/// Creates an NSMutableAttributedString from a C# string and applies the specified CoreText attributes to the entire string.
44+
/// </summary>
45+
/// <param name="str">The string content.</param>
46+
/// <param name="attributes">CoreText attributes to be applied to the string.</param>
47+
public NSMutableAttributedString (string str, CTStringAttributes? attributes)
48+
: this (str, attributes?.Dictionary)
5049
{
5150
}
5251

53-
/// <param name="attributes">To be added.</param>
54-
/// <param name="range">Range to which the attribute will be applied.</param>
55-
/// <summary>Sets the attributes for the specified ranges. Any previous attributes in that range are replaces with the new values.</summary>
56-
/// <remarks>
57-
/// </remarks>
52+
/// <summary>
53+
/// Sets the attributes for the specified range. Any previous attributes in that range are replaced with the new values.
54+
/// </summary>
55+
/// <param name="attributes">The attributes to set.</param>
56+
/// <param name="range">The range to which the attributes will be applied.</param>
5857
public void SetAttributes (NSDictionary attributes, NSRange range)
5958
{
60-
if (attributes is null)
61-
throw new ArgumentNullException ("attributes");
59+
ArgumentNullException.ThrowIfNull (attributes);
6260

6361
LowLevelSetAttributes (attributes.Handle, range);
6462
GC.KeepAlive (attributes);
6563
}
6664

65+
/// <summary>
66+
/// Sets the attributes for the specified range. Any previous attributes in that range are replaced with the new values.
67+
/// </summary>
6768
/// <param name="attrs">CoreText attributes to be set on the string.</param>
68-
/// <param name="range">Range to which the attribute will be applied.</param>
69-
/// <summary>Sets the attributes for the specified ranges. Any previous attributes in that range are replaces with the new values.</summary>
70-
/// <remarks>
71-
/// </remarks>
69+
/// <param name="range">The range to which the attributes will be applied.</param>
7270
public void SetAttributes (CTStringAttributes attrs, NSRange range)
7371
{
74-
SetAttributes (attrs is null ? null : attrs.Dictionary, range);
72+
ArgumentNullException.ThrowIfNull (attrs);
73+
74+
SetAttributes (attrs.Dictionary, range);
7575
}
7676

77+
/// <summary>
78+
/// Adds attributes to the specified range of characters in the string.
79+
/// </summary>
7780
/// <param name="attrs">The CoreText attributes to add.</param>
78-
/// <param name="range">Range to which the attribute will be applied.</param>
79-
/// <summary>Adds an attribute and its value to the specified range of characters in the string.</summary>
80-
/// <remarks>
81-
/// </remarks>
81+
/// <param name="range">The range to which the attributes will be applied.</param>
8282
public void AddAttributes (CTStringAttributes attrs, NSRange range)
8383
{
84-
AddAttributes (attrs is null ? null : attrs.Dictionary, range);
84+
ArgumentNullException.ThrowIfNull (attrs);
85+
86+
AddAttributes (attrs.Dictionary, range);
8587
}
8688

87-
/// <param name="first">To be added.</param>
88-
/// <param name="rest">To be added.</param>
89-
/// <summary>To be added.</summary>
90-
/// <remarks>To be added.</remarks>
89+
/// <summary>
90+
/// Appends an attributed string and additional objects to the end of the receiver.
91+
/// </summary>
92+
/// <param name="first">The first attributed string to append.</param>
93+
/// <param name="rest">Additional objects to append. Can be <see cref="NSAttributedString"/>, <see cref="string"/>, or other objects whose <see cref="object.ToString"/> method will be called.</param>
9194
public void Append (NSAttributedString first, params object [] rest)
9295
{
9396
Append (first);
9497
foreach (var obj in rest) {
95-
if (obj is NSAttributedString)
96-
Append ((NSAttributedString) obj);
97-
else if (obj is string)
98-
Append (new NSAttributedString ((string) obj));
98+
if (obj is NSAttributedString nsAttributedString)
99+
Append (nsAttributedString);
100+
else if (obj is string str)
101+
Append (new NSAttributedString (str));
99102
else
100-
Append (new NSAttributedString (obj.ToString ()));
103+
Append (new NSAttributedString (obj?.ToString () ?? ""));
101104

102105
}
103106
}
104107
#if !MONOMAC
105-
/// <param name="str">To be added.</param>
106-
/// <param name="attributes">To be added.</param>
107-
/// <summary>To be added.</summary>
108-
/// <remarks>To be added.</remarks>
109-
public NSMutableAttributedString (string str, UIStringAttributes attributes)
110-
: this (str, attributes is not null ? attributes.Dictionary : null)
108+
/// <summary>
109+
/// Creates an NSMutableAttributedString from a string with UIKit attributes.
110+
/// </summary>
111+
/// <param name="str">The string content.</param>
112+
/// <param name="attributes">UIKit attributes to be applied to the string.</param>
113+
public NSMutableAttributedString (string str, UIStringAttributes? attributes)
114+
: this (str, attributes?.Dictionary)
111115
{
112116
}
113117

114-
/// <param name="str">To be added.</param>
115-
/// <param name="font">To be added.</param>
116-
/// <param name="foregroundColor">To be added.</param>
117-
/// <param name="backgroundColor">To be added.</param>
118-
/// <param name="strokeColor">To be added.</param>
119-
/// <param name="paragraphStyle">To be added.</param>
120-
/// <param name="ligatures">To be added.</param>
121-
/// <param name="kerning">To be added.</param>
122-
/// <param name="underlineStyle">To be added.</param>
123-
/// <param name="shadow">To be added.</param>
124-
/// <param name="strokeWidth">To be added.</param>
125-
/// <param name="strikethroughStyle">To be added.</param>
126-
/// <summary>To be added.</summary>
127-
/// <remarks>To be added.</remarks>
118+
/// <summary>
119+
/// Creates an NSMutableAttributedString from a string with individual UIKit styling attributes.
120+
/// </summary>
121+
/// <param name="str">The string content.</param>
122+
/// <param name="font">The font to apply to the string.</param>
123+
/// <param name="foregroundColor">The foreground (text) color.</param>
124+
/// <param name="backgroundColor">The background color.</param>
125+
/// <param name="strokeColor">The stroke color for outlined text.</param>
126+
/// <param name="paragraphStyle">The paragraph style to apply.</param>
127+
/// <param name="ligatures">The ligature type to use.</param>
128+
/// <param name="kerning">The kerning value to apply.</param>
129+
/// <param name="underlineStyle">The underline style to apply.</param>
130+
/// <param name="shadow">The shadow effect to apply.</param>
131+
/// <param name="strokeWidth">The stroke width for outlined text.</param>
132+
/// <param name="strikethroughStyle">The strikethrough style to apply.</param>
128133
public NSMutableAttributedString (string str,
129-
UIFont font = null,
130-
UIColor foregroundColor = null,
131-
UIColor backgroundColor = null,
132-
UIColor strokeColor = null,
133-
NSParagraphStyle paragraphStyle = null,
134+
UIFont? font = null,
135+
UIColor? foregroundColor = null,
136+
UIColor? backgroundColor = null,
137+
UIColor? strokeColor = null,
138+
NSParagraphStyle? paragraphStyle = null,
134139
NSLigatureType ligatures = NSLigatureType.Default,
135140
float kerning = 0,
136141
NSUnderlineStyle underlineStyle = NSUnderlineStyle.None,
137-
NSShadow shadow = null,
142+
NSShadow? shadow = null,
138143
float strokeWidth = 0,
139144
NSUnderlineStyle strikethroughStyle = NSUnderlineStyle.None)
140145
: this (str, ToDictionary (font, foregroundColor, backgroundColor, strokeColor, paragraphStyle, ligatures, kerning, underlineStyle, shadow, strokeWidth, strikethroughStyle))

src/Foundation/NSMutableAttributedString.iOS.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@
1111
using UIKit;
1212
using CoreText;
1313

14-
// Disable until we get around to enable + fix any issues.
15-
#nullable disable
14+
#nullable enable
1615

1716
namespace Foundation {
1817

1918
public partial class NSMutableAttributedString {
20-
/// <param name="attrs">To be added.</param>
21-
/// <param name="range">To be added.</param>
22-
/// <summary>To be added.</summary>
23-
/// <remarks>To be added.</remarks>
19+
/// <summary>
20+
/// Sets the attributes for the specified range. Any previous attributes in that range are replaced with the new values.
21+
/// </summary>
22+
/// <param name="attrs">The UIKit attributes to set.</param>
23+
/// <param name="range">The range to which the attributes will be applied.</param>
2424
public void SetAttributes (UIStringAttributes attrs, NSRange range)
2525
{
26-
SetAttributes (attrs is null ? null : attrs.Dictionary, range);
26+
ArgumentNullException.ThrowIfNull (attrs);
27+
28+
SetAttributes (attrs.Dictionary, range);
2729
}
2830

29-
/// <param name="attrs">To be added.</param>
30-
/// <param name="range">To be added.</param>
31-
/// <summary>To be added.</summary>
32-
/// <remarks>To be added.</remarks>
31+
/// <summary>
32+
/// Adds attributes to the specified range of characters in the string.
33+
/// </summary>
34+
/// <param name="attrs">The UIKit attributes to add.</param>
35+
/// <param name="range">The range to which the attributes will be applied.</param>
3336
public void AddAttributes (UIStringAttributes attrs, NSRange range)
3437
{
35-
AddAttributes (attrs is null ? null : attrs.Dictionary, range);
38+
ArgumentNullException.ThrowIfNull (attrs);
39+
40+
AddAttributes (attrs.Dictionary, range);
3641
}
3742

3843
}

0 commit comments

Comments
 (0)